self disable a behavior

hi all,

I’m not sure this is a bug but it looks like one to me. Here is what happened : I wrote a behavior that under certain circumstances (for instance the owner model doesn’t have a specific attribute) is supposed to disable itself. When this behavior is the only one attached to a component, everything is ok, but when a component already has other behaviors and mine is added, the following exception is raised.


Description


Undefined offset:  1


Source File


I:\home\project\lib\php\yii\yii-1.1.5.r2654\framework\collections\CListIterator.php(73)



To reproduce here is a DummyBehavior to create in application/components :




<?php

class DummyBehavior extends CActiveRecordBehavior {

	public function beforeSave($event) {

		$this->enabled=false;

	}

}

?>

… then add it to an existing CActiveRecord model :


<?php

	// ....

	public function behaviors(){

		return array(

			'dummyBehavior' => array(

				'class' => 'application.components.DummyBehavior',

			),

			'dummyBehavior2' => array(

				'class' => 'application.components.DummyBehavior',

			)			

		);		

	}



…and then save the model.

For instance I used this technique to add the zii.behaviors.CTimeStampBehavior to all my models (add behavior to their common base class) and disable it when there is no create or update column defined for a table. As enabled is a public member, it seemed logical …

What do you think ? bug or not bug ?

ciao

I did verify what you say, then moved on to something else. IIRC, more than one behavior (same or different) defined and disabling one of them generates an error message. I didn’t get the same error message, though (MyModel.onbefore…" is attached with an invalid handler “NULL”). Don’t know if this is by design or should be considered a bug.

Edit: typo

/Tommy

thanks for your feedback tri … I feel less alone ;)

Raoul, i also read your post and spent some minutes to investigate the source code. But i couldn’t find a quick explanation.

Just to let you know, that you’re never alone here ;)

Thanks Mike

Maybe when a behavior is disabled it is removed from the array of behaviors attached to a component, and so if this happens from inside the attached behavior itself, the array index is out of bound (need to confirm that).