dqhr76378 2015-07-23 12:44
浏览 121
已采纳

Yii时间戳是如何工作的?

Yii says it can automatically set the timestamps 'created_at': http://www.yiiframework.com/doc-2.0/guide-concept-behaviors.html#using-timestampbehavior

However, every time I created a record, the time is blank:

$ yii kw/test
1437683634

mysql> select * from ad_group_keyword_network where ad_group_keyword_id = 1;
+---------------------+-
| created_at          | 
+---------------------+-
| 0000-00-00 00:00:00 | 
| 0000-00-00 00:00:00 | 
+---------------------+-

  public function actionTest() {
    $agkn = new AdGroupKeywordNetwork();
    $agkn->save();
    echo $agkn->created_at;

I tried with Schema::TYPE_TIMESTAMP and I changed the field and also tried with Schema::TYPE_DATETIME. They both return an integer timestamp in the field after save, but the time is all 0s in the database.

public function behaviors()
{
    return [
        [
            'class' => TimestampBehavior::className(),
            'attributes' => [
                ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ActiveRecord::EVENT_BEFORE_UPDATE => [],
  • 写回答

3条回答 默认 最新

  • duanbangzhou7809 2015-07-23 12:57
    关注

    your created_at must be type INT (11) for the Yii 2 timestamp behaviour work. I think you are using type TIMESTAMP which will not work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?