S
S
Sergey Pugovkin2016-10-22 20:40:28
Yii
Sergey Pugovkin, 2016-10-22 20:40:28

How does the TimestampBehavior work if the field is already populated?

public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'createdAtAttribute' => 'createdAt',
                'updatedAtAttribute' => 'updatedAt',
                'value' => new Expression('NOW()'),
            ],
        ];
    }

Let's say the findOne model is done a second time, and then save it. In this case, $model->updatedAt is already non-null. Will the Behavior ignore it and keep NOW, or will it keep it as it is?
And the second question, dependent on the answer to the first one: how to store the given value, not NOW (not something that is tied to Behavior).

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Pugovkin, 2016-10-22
@Driver86

Clear.
The nuts are tightly tightened there - the value specified in behavior is set.
detachBehavior solves the second question

A
Andrew, 2016-10-22
@mhthnz

On update , the current timestamp will be stored in updated_at . You can pass your value as a function, or as a string:

[
                'class' => TimestampBehavior::className(),
                'value' => date('Y.m.d H:i'),
            ],

            [
                'class' => TimestampBehavior::className(),
                'value' => function($event) {
                    return date('Y.m.d H:i');
                },
            ],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question