CTimestampBehavior - createAttribute reverts to zero on update.

Now, this has just been noticed; that when a record is updated, the create_time is reset to 0000-00-00 00:00:00, and the update_time is correct.

The create_time is also fine, if you create a new record - just not if you update it,.

Is this a common thing? I’m asking before i tear my hair out, fault finding…

:-X

Model:


public function behaviors(){

	    return array(

		    'CTimestampBehavior' => array(

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

			    'createAttribute' => 'create_time',

			    'updateAttribute' => 'update_time',

		    )

	    );

    }

Normally it works and the behavior doesn’t touch the create_time attribute during update (you can check its code to be sure).

Maybe you have added a rule for the create_time attribute so it’s updated bymassive assignment or in beforeSave, and are converting it in afterFind or equivalent?

I’m basically thinking that the attribute value is somewhat perverted from the rigid SQL datetime format so it’s saved as default empty value.

hey bennouna,

I’ve got an afterFind… could that be screwing with it?


protected function afterFind ()

    {


	// convert to display format

        $this->create_time = strtotime ($this->create_time);

        $this->create_time = date ('d/m/Y H:i', $this->create_time);

		

	$this->update_time = strtotime ($this->update_time);

        $this->update_time = date ('d/m/Y H:i', $this->update_time);

...



Im just searching through all files to see whats what…

Of course, that’s what was saying. You have to convert create_time again in beforeSave, like this:


list($c_date, $c_time) = explode(' ', $this->create_time);

list($c_d, $c_m, $c_y) = explode('/', $c_date);

list($c_h, $c_i) = explode(':', $c_time);

$this->create_time = date('Y-m-d H:i:s', mktime($c_h, $c_i, 0, $c_m, $c_d, $c_y));

Hey, I was just about to post that i removed that afterSave code, and it worked perfectly…

So, i’ve used your code, and the problem appears to be cured!!! The create time isnt touched, update is changing, and the view format is perfect.

I really do owe you a few beers now, i hope youre keeping count!

Thank you

p :D

Welcome P. I’m glad it could help.

Let’s make them soft drinks or espressos :)

Strange:

in the DB the create_time = 2012-05-22 14:21:09

but on the form view it’s 2005-06-03 14:21:00

:blink:

UPDATE:

All the records appear to have a created date of 2005-06-03 in the view (DB is fine), so i commented out your List code, and the correct dates show, but obviously in the undesired format, so I need to re vist what i was playing with in my admin page CGridView for example:




array(

// display 'create_time' using an expression

  'name'=>'create_time',

  'value'=>'date("M j Y", $data->create_time)',

),



which currently shows all dates as 1970, so will play with date functions until it shows the actual date.

Ideally, i want to set the date format somewhere, and it be set on all views etc…

That’s because you format your date without seconds in afterFind…

If you change it to date(‘d/m/Y H:i:s’, …) don’t forget to change also the code in beforeSave!