yii2


Difference between afterSave and a Event in yii2?


I wanted to send an email to admin when a new user registers. I think i can do it using two ways. one way is to use events and other is by using afterSave.
By using Events
Controller code
public function actionCreate()
{
$model = new Registeration();
if ($model->load(Yii::$app->request->post()))
{
if($model->save())
{
$model->trigger(Registeration::EVENT_NEW_USER);
}
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
Model code
const EVENT_NEW_USER = 'new-user';
public function init(){
$this->on(self::EVENT_NEW_USER, [$this, 'sendMail']);
}
public function sendMail($event){
// code
}
I can do the same using the afterSave method
Model code
public function afterSave($insert)
{
//code
return parent::afterSave($insert);
}
So is there any difference between the two methods? Which one is better using Events or afterSave() ?
I am new to Yii,
It depends on what you are trying to implement.
When you use afterSave email will be sent on updates also.
So event would be a better choice to your problem.
Thanks & Regards
Paul P Elias

Related Links

Yii2 UUID not mapped with model value
Yii2 Pjax on ActiveForm and GridView [ Got it Working]
Yii2 - how to get the selected value from DepDrop
How to use models from different domain in frontend's site controller?
frontend login is not working properly in yii2
Yii Gridview show/hide columns from interface when filtering the records
Yii2 skip to other URL route if <controller>/<action> is not found
Include additional library to yii2
Yii2 make session works on all actions of one controller?
Yii2 display data from phpexcel in view before saving to database
yii2 dependant dropdown reset on edit action due to onchange function
Yii2 non-DB (or virtual) attribute isn't populated during massive assignment?
Yii2 Join multiple tables
Codeception Adding custom assert actions (functional testing)
Selected values in yii2
Yii queryBuilder; If dbexpression is not being quoted well

Categories

HOME
google-chrome-extension
winforms
variables
proxy
deezer
homebrew
fft
microservices
gps
ckeditor
retrofit
ojdbc
azure-media-services
paradox
pheatmap
cross-validation
kibana-4
google-cloud-ml
usergrid
windows-phone-7
interop
sms-gateway
nhibernate-envers
crystal-reports-2008
scichart
nodatime
qhull
fish
bootstrap-material-design
linkerd
gsmcomm
unboundid
twilio-api
wixsharp
devextreme
hockeyapp
swift3.0.2
libraries
theano.scan
uft-api
hue
temporary-files
elasticsearch-plugin
optix
estimote
sas-jmp
babel-core
avconv
hibernate-tools
upstart
multipeer-connectivity
theming
python-webbrowser
datastax-startup
alphabet
pebble-watch
return-value
setter
csound
ifs
flow-control
mcafee
titanium-android
webdriverjs
account-kit
gstreamer-0.10
make-install
pickadate
php-internals
ipconfig
associative-array
metaclass
php-ci
census
dereference
offloading
nstableviewcell
jscript.net
zend-route
wp-query
expected-exception
braille
message-driven-bean
stxxl
multiprocessor
easy-install
jquery-mobile-dialog
mqx
cassini-dev
specification-pattern
angularjs-controller
trusted
pysimplesoap
hirefire
joyent
tomcat-valve
netbeans-7.1
locate
web-application-design
ext3
firefox4
audio-capture
data-acquisition

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App