[solved] TimestampBehavior destroys created_at value on update?

Hi Guys!

I just realized a strange problem with TimestampBehaviour in my models.

The situation:

[list=1]

[*]When I create a new record both timestamps are set correctly.

[*]When I update an existing record "updated_at" is set correctly but "created_at" gets destroyed and has a strange (random) value like: 1, 20 or 22…

[/list]

So far I was not able to figure out why this happens.

My configuration / implementation:

I use mySQL with InnoDB.

The timestamp fields are setup like this:

created_at => int(11)

updated_at => int(11)

My implementation of timstamp behavior in model:




use yii\db\ActiveRecord;

use yii\behaviors\TimestampBehavior;


... ... ... 

public function behaviors()

{

    return [

        [

            'class' => TimestampBehavior::className(),

            'attributes' => [

                ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],

                ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],

            ],

        ],

    ];

}



Like I said this only happens on created_at field when I want to update a record.

The updated_at field is updated corretly to current timestamp.

On new created records both timestamps are set correctly.

Any Ideas why this happens?

Thanks and best regards

Okay…

Problem was caused by own stupidity.

Timestamp Behavor is working fine.

It was caused because I did this in some models:




public function afterFind(){

    parent::afterFind(); 


    $this->created_at = date('d.m.Y - H:i:s', $this->created_at);

    $this->updated_at = date('d.m.Y - H:i:s', $this->updated_at);

}



I guess the locigal reason is:

afterFind gets also triggered when you update a record.

and because on update "created_at" is not touched it gets the wrong value from the formatting I have done there… Make sense to me.

Regards