青山不知 2021-04-16 10:12:38 5190次浏览 0条评论 0 0 0

通常设置默认值直接在 defaultValue 中设置就好,但是 defaultValue 中传入字符串会被自动加入 引号 或者 代字号,在需要设置一些数据库特殊字符时,需要使用 new Expression("command") 转义,在migrations语法中如下表示。

$this->createTable($this->tab_name, [ 'id' => $this->primaryKey(), 'c_time' => $this->timestamp(0)->defaultValue(new \yii\db\Expression('CURRENT_TIMESTAMP'))->notNull()->comment('创建时间'), 'u_time' => $this->timestamp(0)->defaultValue(new \yii\db\Expression('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'))->notNull()->comment('修改时间')), ], 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT=\'用户表\''); 
    没有找到数据。
您需要登录后才可以评论。登录 | 立即注册