Model check controller action caller

Ia have this in controller:


and this on the model:

public function afterSave(){



How can i detect if the aftersave is called by action reate or action update?

if ($this->isNewRecord) {}

Or you can just read the action name exactly






yes but i give up on the idea because we shoudnt acess controller instance by the model right?

What mbi explained should do the trick, i mean, if $this->isNewRecord===true it means it is a create action, otherwise it is an update action.

There’s no reason why you are not allowed to access the controller instance from the model.

Models should do all the hard work, for instance, if after a user registers to your site you want to send an html email (and you don’t use a behavior for this to hook into afterSave()) then you would need to CController::renderPartial() the html content of the email, and you can do this only by accessing the controller instance, ie:



   if(Yii::app()->controller->id='user' && Yii::app()->controller->action->id=='register')






This is just an example, but the thing is you often need to access the controller from models or widgets or any other components.