【发布时间】:2017-01-28 17:39:33
【问题描述】:
我想使用 yii2 ActiveRecord 在我的表中插入多条记录。 我已经知道我可以使用这段代码了
$connection->createCommand()->batchInsert('user', ['name', 'age'], [
['Tom', 30],
['Jane', 20],
['Linda', 25],
])->execute();
但是通过这种方法,我的模型验证没有执行。 我已经读过这个问题 ActiveRecord batch insert (yii2)
但也通过以一种棘手的方式进行验证,考虑我想使用 ActiveRecords 事件填充 created_at 和 updated_at 列。
就这样
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
if($insert)
$this->created_at = date('Y-m-d H:i:s');
$this->updated_at = date('Y-m-d H:i:s');
return true;
} else {
return false;
}
}
【问题讨论】:
标签: database activerecord insert yii2