Yii2一次保存多个数据记录

时间:2016-03-07 21:00:37

标签: php activerecord yii2

如何一次保存多个数据记录,例如

$model->title = "Negativ Balance";
$model->settingsName = "negativeBalance";
$model->settingsValue = "0";
$model->save();

$model->title = "Warnung Balance";
$model->settingsName = "warningBalance";
$model->settingsValue = "2";
$model->save();

如果我执行这些代码,只保存最后一条记录,如何保存所有这些数据?谢谢!

1 个答案:

答案 0 :(得分:1)

您必须创建一个新模型,然后分配值

update

但是如果您需要多次插入,那么您还应该查看$model = new YourModel(); $model->title = "Negativ Balance"; $model->settingsName = "negativeBalance"; $model->settingsValue = "0"; $model->save(); $model = new YourModel(); $model->title = "Warnung Balance"; $model->settingsName = "warningBalance"; $model->settingsValue = "2"; $model->save(); batchInsert()方法,了解更多http://www.yiiframework.com/doc-2.0/yii-db-command.html#batchInsert%28%29-detail