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

Creating a one-to-many relationship in Yii2
Handle Image update in yii2
Header and Footer missing in modal popup
Select2 Krajee widget - using modal and getting the id of the value
sjaakp/yii2-illustrated-behavior Yii2 extension for image not working
change the navigation bar url from yii2 without redirecting
Yii 2 - Conditional Assets?
How to get value of dropdown instead of id yii2
How can I access private value from object
How to add valueless attribute to <form> tag using yii2 ActiveForm
Register a js file independently in Assets
Get user's email from twitter login in yii2
get Attribute Label in yii2
Yii 2 code formater to display xml with code highlighting in html, string highlighter
Change Breadcrumb URL for one module in yii2
yii2 multi step form

Categories

HOME
java
sendgrid
hook
mediawiki
mockito
single-sign-on
smarty
relative-path
hp-exstream
vmware
sd-card
octobercms
umd
callback
enterprise-library-5
esper
adfs
applepay
spring-xd
numeral.js
google-cloud-spanner
opentracing
saxon
google-pagespeed
immutable.js
conemu
kvc
mustache.php
autosys
dxf
brunch
fgetcsv
windows-server-2000
tasklet
bosh
yadcf
galsim
.net-4.0
xquery-3.0
code-contracts
gammu
force-layout
picasso
ios5
git-merge
withings
xenforo
angularjs-factory
greenrobot-eventbus
unoconv
nand2tetris
eclipse-gef
flickr-api
azure-application-gateway
pebble-watch
powercli
brightcove
google-perftools
pearson
jspdf-autotable
vtigercrm
nested-sets
time-and-attendance
gridpane
libpng
sts-springsourcetoolsuite
home-directory
url-masking
goose
asp.net5
jfugue
cyclomatic-complexity
jqgrid-formatter
android-listview
tableau-online
sysinternals
apache-commons-fileupload
web-controls
unity5.2.3
p2
dereference
jsonpickle
file-copying
preferences
soundtouch
easy-install
django-nonrel
jquery-mobile-dialog
first-responder
propertyeditor
pysimplesoap
twrequest
ocx
qtkit
bubble-chart
netdna-api
pydot
genshi
xmlspy
servicehost
dbal
firefox-5
libs
exchange-server-2003
ctp4
boost-smart-ptr
privilege
geneva-server
lzh

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