G
G
Greg Popov2016-06-13 17:17:25
Yii
Greg Popov, 2016-06-13 17:17:25

What is the error of using TimeStampBehavior in Yii2?

Hello.
When creating a record and when updating, we log the time

return [
            'timestamp' => [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => 'created_at',
                    ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
                ],
                'value' => function() { return date('U'); },
            ],
        ];

As a result, it erases the creation dout.
PS this
return [
    'class' => TimestampBehavior::className(),
        'attributes' => [
           ActiveRecord::EVENT_BEFORE_INSERT => ['create_time', 'update_time'],
           ActiveRecord::EVENT_BEFORE_UPDATE => ['update_time'],
    ],
  ];

gives the same result, just
return [
   TimestampBehavior::className(),
  ];

produces the same clumsy result.
Where is the mistake?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Elena Stepanova, 2016-06-13
@Insolita

return [
    'class' => TimestampBehavior::className(),
     'createdAtAttribute' => 'created_at',
     'updatedAtAttribute' => 'updated_at',
      'value' => function () {
                    return date('Y-m-d H:i:s');
      }
  ];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question