Dear Rajendri
$demoModel = Demo::model();
$demoModel->detachBehavior('autoFormatDatetime');
$demoModel->disableBehavior('autoFormatDatetime');
$demoModel->disableBehaviors();
The above code only is going to detach the attached behaviors from the static instance, $demoModel.(Correct me if I am wrong).
It is not going to remove the attached behaviors when it instantiate a row in table
with find methods.
That is why I feel that results are not surprising, When you make the following code.
$demo= $demoModel->findByPk(1);
The extension you are talking about is derived from CActiveRecordBehavior.
It has got two methods and they are attached to two events of CActiveRecord.
-
“onBeforeSave” => ‘beforeSave’
-
“onAfterFind” => ‘afterFind’
(these methods in Behavior class should not be confused with the methods with same name in AR class)
We can detach the autoFormatDateTimeBehavior by overriding the afterFind method of CActiveRecord.
1.By simply declaring the method without doing any thing. This way we bypass the event onAfterFind
public function afterFind()
{
}
The disadvantage here is that we can miss other handlers attached to the event onAfterFind.
This may give undesirable side effects.
To avoid that we have to call the parent method. At the same time we have to detach the undesired behaviors.
-
The following code detaches behavior and calls the parent method.Then it again attaches the behavior.
This way we can avoid problems when we are updating the record. This momentarily disables the behavior.
public function afterFind()
{
$behavior=$this->asa('autoFormatDatetime');
$this->detachBehavior("autoFormatDatetime");
parent::afterFind();
$this->attachBehavior("autoFormatDatetime",$behavior);
}
3.I think the more elegant way is only to detach the particular event handler not the whole behavior.
This also ensures that it is not going to affect the event onBeforeSave.
public function afterFind()
{
$behavior=$this->asa('autoFormatDatetime');
$this->detachEventHandler('onAfterFind',array($behavior,'afterFind'));
parent::afterFind();
}
Ofcourse we can again reattach the event. But it is not needed in our case.
public function afterFind()
{
$behavior=$this->asa('autoFormatDatetime');
$this->detachEventHandler('onAfterFind',array($behavior,'afterFind'));
parent::afterFind();
$this->attachEventHandler('onAfterFind',array($behavior,'afterFind'));
}
I expect more inputs on this.
Regards.