Model Save And Automated Timestamps

I have a field in my database(MySQL) table of

type:TIMESTAMP with

default value:CURRENT_TIMESTAMP and

attributes : on update CURRENT_TIMESTAMP

Normally , when I update any field in the table (lets say by hand or via plain php code) , the Timestamp field gets automatically updated.This is normal MySQL behavior.

Although , when a model gets saved via Yii ( $model->save() ) the specific Timestamp field doesn’t get updated.

Is the model->save() action not just a simple mysql update action ?

Any clues why this is happening ?

Hi,

please see it…

http://www.yiiframework.com/doc/api/1.1/CTimestampBehavior/

it may be helpful…

need two fields

created time and updated time

Thanks for the replies , but this doesn’t answer my question , why a typical MySql behavior (something that is in table-level) gets corrupted via Yii.

I tried to write to the table via plain PHP (mysqli commands) and everything worked as expected.

I don’t want to test what would happen if I had triggers on my tables.

Would they be ignored too on model-save() ???

Don’t really know the answer to your question exactly however, I use a before save function in my model and don’t ever have a problem.

Note if you you used gii initially then you and added some fields later for some reason it doesn’t register the new fields as safe even if you use gii to save over the current model. So make sure they are marked as safe like:

ie.




	public function rules()

	{

		return array(

			array('creator, create_date, updater, update_date', 'safe'),

		);

	}



Here is something similar to what i use in my before save in my models




public function beforeSave()

	{

		if ($this->getIsNewRecord())

		{

		            	$this->create_date = new CDbExpression('NOW()');

 		                if (!Yii::app()->user->isGuest) {           		

                              	$this->creator = Yii::app()->user->name;   

                      	}

        	            

                            else {

       			              	$this->creator = 'Guest';   

         	               }

		}

		else {

			$this->update_date = new CDbExpression('NOW()');

			$this->updater = Yii::app()->user->name;

		}

	return parent::beforeSave();

	}



I don’t have those fields on my forms just let the above do it

Hi,

you can put code in your model





public function behaviors(){

	return array(

		'CTimestampBehavior' => array(

			'class' => 'zii.behaviors.CTimestampBehavior',

			'createAttribute' => 'create_date',

			'updateAttribute' => 'update_date',

		)

	);

}

is this automatic insert and update when form is fill up

Note:But make sure in DB creta_date and update_date type is TIMESTEMP

Thanks again to all of you.

I finally used this approach :

Inside my model , in function rules




public function rules()

{

    return array(

        array('title','length','max'=>255),

        array('title, created, modified', 'required'),




        array('modified','default',

              'value'=>new CDbExpression('NOW()'),

              'setOnEmpty'=>false,'on'=>'update'),

        array('created,modified','default',

              'value'=>new CDbExpression('NOW()'),

              'setOnEmpty'=>false,'on'=>'insert')




    );

}



Found it here–>http://www.yiiframework.com/wiki/10/how-to-automate-timestamps-in-activerecord-models/

It’s doing the job :rolleyes:

Hi

it’s very nice …some one help it…