Weird behavior with TimestampBehavior

In my model it uses the TimestampBehavior class




[

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

    'updatedAtAttribute' => 'featured_on',

]

When the query ran, it ran this


UPDATE `theme` SET `featured_on`='1532025711', `rating`='3', `css`='sh: 1: sassc: not found', `review_count`=1 WHERE `id`=227

In MySQL 5.7 with strict settings enabled this generates an error


Invalid datetime format: 1292 Incorrect datetime value: '1532025711' for column 'featured_on'

The field definition is




+---------------+--------------------------------------------+------+-----+-------------------+-----------------------------+

| Field         | Type                                       | Null | Key | Default           | Extra                       |

+---------------+--------------------------------------------+------+-----+-------------------+-----------------------------+

| featured_on   | timestamp                                  | YES  |     | NULL              |                             |

+---------------+--------------------------------------------+------+-----+-------------------+-----------------------------+

The weirder thing is the default value for the property value of the TimestampBehavior is new Expression(‘NOW()’)

When I explicitly set the value property it works




[

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

    'updatedAtAttribute' => 'featured_on',

    'value' => new Expression('NOW()'), // using CURRENT_TIMESTAMP also works

]



Hi, if the value property is null the default value of TimestampBehavior::$value is set using php’s time() function




// https://github.com/yiisoft/yii2/blob/master/framework/behaviors/TimestampBehavior.php#L116

protected function getValue($event)

    {

        if ($this->value === null) {

            return time();

        }

        return parent::getValue($event);

    }

You have to explicitly set the value to expression just like your last snippet

What if you try this


UPDATE `theme` SET `featured_on`=1532025711, `rating`='3', `css`='sh: 1: sassc: not found', `review_count`=1 WHERE `id`=227

instead of this


UPDATE `theme` SET `featured_on`='1532025711', `rating`='3', `css`='sh: 1: sassc: not found', `review_count`=1 WHERE `id`=227

?

I’m not creating the query. It’s being created by the ActiveRecord save. The issue stems from strict mode. Which isn’t mentioned in the docs and I wasn’t aware that one of our test servers was using strict mode.

The docs mention using new Expression(‘NOW()’) if you’re using a datetime field, which I am not. I’m using a timestamp field and in a non-strict mode on my machine for development MySQL excepts a UNIX timestamp as a timestamp value.

It comes down to the class works as expected, the documentation is missing a small, but important piece of information.