Attaching behaviors during OnBeginRequest event

Hi to everybody,

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 :D




$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');

	}	

	

}



How about within /protected main/config.php? :




...

'import'=>array(),

....

'behaviors'=>array(

     ' path/to/moduleBehaviors',

),

...

'modules'=>array(),

'components'=>array();



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

Thanks for your answer Seal.

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.

Cristian :D

Ha, fair play. Nothing comes to mind right now.

Nonetheless I found a quick and dirty solution wich requires adding one item under the ‘params’ array located in config.php…




...

   'params' => array(

   ...

       'behaviors' => array(),

   ...

   ),

...



… deriving all my application models from the same parent implementing the following:




class CustomActiveRecord extends CActiveRecord

{

	public function behaviors()

	{		 

		return Yii::app()->params->behaviors[get_class($this)];	 

	}	

}



…and populating the array when application onBeginRequest is being fired (as I wrote in the first post).

But I’m not entirely satisfied with this solution.

Cristian