【问题标题】:Why Yii2 dateTime() functionality doens't writes the time?为什么 Yii2 dateTime() 功能不写时间?
【发布时间】:2018-10-11 16:07:20
【问题描述】:

我已经使用 Gii 生成了 CRUD,但我的迁移没有按预期工作。

'id' => $this->primaryKey(),
'student' => $this->string(255)->notNull(),
'diploma_teacher' => $this->string(255)->notNull(),
'type_of_work' => $this->string(255)->notNull(),
'student_classification' => $this->string(255)->notNull(),
'title' => $this->string(255)->notNull(),
'created_at' => $this->dateTime()->notNull(),
'updated_at' => $this->dateTime()->notNull(),

当创建新的 Diploma Work 字段时 created_at 和 updated_at

0000-00-00 00:00:00
0000-00-00 00:00:00

【问题讨论】:

    标签: php mysql model-view-controller yii yii2


    【解决方案1】:

    我建议您将create_atupdate_at 声明如下,

    'create_at' => $this->dateTime()->notNull()->defaultExpression('now()'),
    'update_at' => $this->dateTime()->notNull()->defaultExpression('now()')->append('ON UPDATE CURRENT_TIMESTAMP')
    

    【讨论】:

    • 对 MySQL 更正
    【解决方案2】:

    你应该添加默认值表达式

    'created' => $this->dateTime()->notNull()->defaultExpression('now()')
    

    【讨论】:

      猜你喜欢
      • 2018-08-19
      • 2019-10-29
      • 2013-05-03
      • 1970-01-01
      • 1970-01-01
      • 2012-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多