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

PendalF89/yii2-filemanager upload error. (Call to a member function saveAs() on null)
Yii2-user Dektrium - how to hash password
2amigos Datepicker is not working with AdminLTE in yii2
Custom filter DD for gridview yii2
Cannot update an extension to latest version in yii2
checkboxlist not rendering checkbox checked correctly
Differentiate between Frontend and Backend Logs in DBTarget Yii2
yii2 ActiveData Provider
How to use custom php function to filter in ActiveDataProvider
Yii2 sort object defaultOrder
How to display names instead of id's in GridView Yii2?
yii2 / Migration Not Working
Kartik GridView Yii2 : Toggle data not working in toolbar
yii2 http client request and repsponse
How to insert multiple times the value of an attribute in single table in yii2
yii2 SluggableBehavior is applied to only emty slug field

Categories

HOME
kde
angular-material
hp-exstream
spagobi
framework7
google-docs
cvs
v8
dxl
yum
bs4
upload
maude-system
metatrader4
modx-revolution
iron-router
carthage
nodatime
conemu
paging
cloudhub
csrf-protection
facebook-instant-articles
accessor
opennlp
microsoft-sync-framework
su
brunch
traffic
uninstall
fopen
jaxb2
sql-server-2012-express
directx-10
create-table
google-sites-2016
framemaker
protovis
google-api-nodejs-client
mmenu
bitbucket-pipelines
web-mining
uft-api
android-tabhost
fedex
reportingservices-2005
checkboxlist
filepicker
pango
datastax-startup
powercli
statsd
mplayer
jxcore
diagnostics
kbuild
dtexec
namecoin
mcafee
crosswalk-runtime
grails-tomcat-plugin
django-debug-toolbar
fuzzy-search
make-install
rvest
pickadate
dataview
two-factor-authentication
historian
pervasive-sql
execute
python-3.2
libressl
ibaction
splash
apache-commons-fileupload
modalpopup
operator-precedence
id3v2
flask-cors
project-planning
dml
system.net.webexception
bundles
concurrent-collections
pyhdf
dotnetnuke-5
dataadapter
smtp-auth
broken-links
regsvr32
reddot
amazon-appstore
php-parser
xdomainrequest
floating
mozilla-prism
modelstate
nsviewanimation
fixed-width
sef
noscript
nerddinner
defensive-programming
geneva-server

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