I’m pretty new to this forum even though I have been using Yii for almost a year.
Here is my problem.
Hoping I could attach some custom behaviors I previously had defined inside a module, I modified index.php as follows, but it seems that only onBeforeFind is fired.
I haven’t been lucky in finding documentation about this issue and that’s why I would like to know wheter what I’m looking for is achievable or not and wheter any of you has encountered the same problem hopefully finding a solution.
Many thanks in advance
$app = Yii::createWebApplication($config);
$app->attachEventHandler('onBeginRequest', 'manageBehaviors');
$app->run();
function manageBehaviors ($event)
{
Yii::import($relativePathToBehaviorClass)
CActiveRecord::model($modelName)->attachBehavior('behaviorName', new $behaviorClass);
}
Class used to define behaviors:
class moduleBehaviors extends CActiveRecordBehavior
{
public function beforeFind($event)
{
Yii::log('beforeFind', 'info', 'urlfilter.behaviors.moduleBehaviors ');
}
public function afterFind($event)
{
Yii::log('afterFind', 'info', 'urlfilter.behaviors.moduleBehavior');
}
public function beforeSave($event)
{
Yii::log('beforeUpdate', 'info', 'urlfilter.behaviors.moduleBehaviors');
}
public function afterSave($event)
{
Yii::log('afterSave', 'info', 'urlfilter.behaviors.moduleBehaviors');
}
public function beforeValidate($event)
{
Yii::log('beforeValidate', 'info', 'urlfilter.behaviors.moduleBehaviors');
}
}
Another clue, judging by your code, you want to log before an after accessing database related functions. One thing I do notice is that you are not passing control back to the parent activeRecord e.g return parent::xyz or return true. this way the parent will know that event are valid and continues
I know that I can add behaviors modifying the main config file, but that’s in fact what I would like to avoid because it would require me to change config.php for each installed module.
I am looking for a way to attach any behavior class a module can provide to any of the models my application relies on, keeping the best mainteinability I can afford.
I also think that behaviors methods should not contain any reference to the parent.
It is the model itself, indeed, that should return correctly thus raising the event.