Detaching Behavior Before Finding

Would appreciate if someone can help me out here.

I need to detach the auto format date time behavior so that it is not converted afterFind(). Tried all ways of doing it but the datetime is always formatted via the behavior.


                

$demoModel = Demo::model();

$demoModel->detachBehavior('autoFormatDatetime');

$demoModel->disableBehavior('autoFormatDatetime');

$demoModel->disableBehaviors();

                

$demo= $demoModel->findByPk(1); //Still this is auto formatted, i don't want that



This post is very similar to my question. It leaves no clue on how to do it. Is there a way? Or do i have to use CDbCommand to fetch all the attributes manually. Thanks~! :)

UPDATE…

Currently i do it this way still hoping for a fix…


                

$dataReader = Yii::app()->db

        ->createCommand("SELECT " . implode(',', array_keys(Demo::model()->getAttributes())) . " FROM demo WHERE demo_id=:demoId")

        ->query(array(':demoId'=>$demoId));


$demo = new demo();

if ($demoAttributes= $dataReader->read())

{

    $demo->attributes = $demoAttributes;

}



Why not just remove the behavior completely and use a custom accessor to retrieve the formatted value?

I can’t the web end uses it intensively, i am using a script to update from the mobile end.

You cant stop an afterfind behavior.

Follow the advice of Keith, is not a good idea to have a behavior that replace the db value with another one, custom accessor are much better.

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.

  1. “onBeforeSave” => ‘beforeSave’

  2. “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.

  1. 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.