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

Login on advanced template with frontend and backend
Why does CSRF get validated when executing console command in Yii2
Yii2: Add 'user_id' when create a post
output of active view depends on the type of user
How to set-up different URLs for yii frontend and backend
Ecommerce/Classified template with Yii2
Yii2 field accessed only via magic method
How to add a css class to <td> tags in a kv- GridView class in Yii2
Capcha always show incorrect
Integration Reportico in Yii2 Projects
Yii2 : Subdirectory in yii2 not working
Yii2 KCFinder with CKEditor
Yii2 - validation compare acting strange
Updating composer throws exception Class Fxp\Composer\AssetPlugin\Repository\NpmRepository does not exist
Dynamic declaration of a model in Yii2
image are save in folder but not saved in database in yii2.0

Categories

HOME
testing
date
omnet++
azure-data-factory
path-finding
lodash
relayjs
cplex
cvs
webpack-2
qore
amazon-cloudformation
node-pdfkit
fancybox-3
spring-kafka
imacros
gz
phaser
caml
pc
evopdf
alpine
immutable.js
conemu
cloudhub
functional-testing
internet-explorer-9
traffic
saas
log4js-node
compatibility
gsoap
r-raster
stormpath
textmate
subset-sum
accelerate-framework
html5-fullscreen
wpf-controls
unspecified
uft-api
cookiecutter-django
grid.mvc
ibpy
fakeiteasy
midl
parentheses
worksheet
winscp-net
return-value
sqldf
eventkit
wdf
ado.net-entity-data-model
storekit
gstreamer-0.10
clang-static-analyzer
simplewebrtc
actionbardrawertoggle
hover-over
energy
xna-4.0
prerequisites
intel-fortran
tmuxinator
0xdbe
apache-commons-fileupload
csquery
website-monitoring
gadt
file-copying
terminfo
project-planning
phpthumb
industrial
tidy
ruby-datamapper
gwt-rpc
mylyn
hungarian-algorithm
plasma
ocunit
dice
multipage
php-parser
nsobject
zpt
external-accessory
tomcat-valve
invite
yetanotherforum
coda-slider
compiler-specific
backcolor
phonon
sustainable-pace
mdac

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