Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Yii2 timestamphavior返回';创建于';不';没有默认值错误_Yii2 - Fatal编程技术网

使用Yii2 timestamphavior返回';创建于';不';没有默认值错误

使用Yii2 timestamphavior返回';创建于';不';没有默认值错误,yii2,Yii2,试图使用Yii2TimestampBehavior但在保存时将返回一个没有默认值的错误 规则中不需要这些值。DB列是一个INT(11)。 由于需要保存几行,因此我使用了batchInsert() 当我用time()添加一个值时,它会将所有内容保存到数据库中 public function behaviors() { return [ 'timestamp' => [ 'class' => TimestampBehavior::className

试图使用Yii2
TimestampBehavior
但在保存时将返回一个
没有默认值的错误

规则中不需要这些值。DB列是一个
INT(11)
。 由于需要保存几行,因此我使用了
batchInsert()

当我用
time()
添加一个值时,它会将所有内容保存到数据库中

public function behaviors()
{
  return [
       'timestamp' => [
           'class' => TimestampBehavior::className(),
           'attributes' => [
               ActiveRecord::EVENT_BEFORE_INSERT => 'created_at',
               ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
           ],
           'value' => new Expression('NOW()'),
       ],
       'blameable' => [
           'class' => BlameableBehavior::className(),
           'createdByAttribute' => 'created_by',
           'updatedByAttribute' => 'updated_by',
        ],
   ];
}

Yii::$app->db->createCommand()->batchInsert( 'table_cms',
     [
        //all fields but not the timestamps
     ],
     [
        //rows here
     ])
     ->execute();
batchInsert()
不支持ActiveRecord功能,因此
TimestampBehavior
在这里无效<如果调用
save()
,将使用code>TimestampBehavior
。在您的情况下,您需要手动传递时间戳


没有默认值
可能意味着您在严格模式下使用MySQL-如果您试图添加记录而不指定所有没有默认值的字段,它将警告您。

好的,我担心batchInsert不支持该行为,我将手动执行。感谢您提供的信息:-)