Detach Event

Is there a way to detach the onAfterSave event from an active record?

Or at least prevent it from being raised?

The only way I can think of is by extending CActiveRecord and overriding the onAfterSave method. Can you accomplish whatever it is you’re trying to do by writing conditional code for the event?

I want to use the updateCounters method to increment a value but i don’t want it to call the onAfterSave event.

if was thinking about settings a flag for example



then in my model

protected function afterSave()





        $this->onAfterSave(new CEvent($this));




is it valid to approach my problem this way?

There is a detachEventHandler() but not sure how to use it.

I don’t think you need to override the afterSave method in your case. Use the flag property in the event method like so

public function afterSave($event) {


  	//do something


  	//do nothing


Your way will get the job done as well.

thanks a lot for the input.

Actually this would be the effect of declaring protected afterSave() in a AR model, replacing return parent::afterSave() with return true (or false). I don’t say this would be a good solution since you may miss some parent afterSave() action that actually should take place.

Normally, all parents afterSave() will be called, then the event will be raised (and any behaviors’ public afterSave() will be called.


I also think so.