Multiple behaviors with afterSave() attached a model class

If multiple behaviors with afterSave() attached a model class, which one will take effect? Or all of them? If latter, what is the order? Thanks!

Seems that only the last attached one is invoked

You can try add the parent reference:

protected function afterSave(){

parent::afterSave();


....your code

}

But a behavior’s parent is not an instance of CActiveRecord

Use $this->owner->afterSave();

So what’s the best way to get all of the behaviors afterSave() (or in my case, both beforeSave() AND afterConstruct()) working?

The best I can fathom is rename the behavior’s methods and then call those methods from the model’s beforeSave() (et al), but I’m hoping for something better…

All the behaviors attached to a model will be executed in the order they have been attached.

For before* methods (for behaviors) the signature needs to look like:




public function beforeSave($event)

{

   //execute code here


   return parent::beforeSave($event);// be sure you have this line.

}



for after* methods:




public function afterSave($event)

{

   //execute code here


   parent::afterSave($event); // don't forget this line.

}



Same thing goes for model based events but without having to pass the $event instance.

As long as you follow this convention, all your behaviors attached to a model will be executed.

The behavior flavour of before/after methods don’t use return values. (The corresponding protected model methods do, when applicable)

I have not used this yet, but it seems like the status should be communicated by the events isValid property.

/Tommy

Even though I’m only using two 3rd party behaviors, it’s strange that none of the ones I’ve downloaded actually use this convention.

Thanks for your help.