Yii2批量插入数据
方法一
yii2一次插入多行数据
/**
* @inheritdoc 批量添加
* @params $add array 添加数据
*/
public function add_all($add)
{
$connection = \Yii::$app->db;
//数据批量入库
$connection->createCommand()->batchInsert(
'zss_stat_series',
['series_name','series_turnover','created_at'],//字段
$add
)->execute();
}
方法二
$model = new User();
foreach($data as $attributes)
{
$_model = clone $model;
$_model->setAttributes($attributes);
$_model->save();
}
方法三
$model = new User();
foreach($data as $attributes)
{
$model->isNewRecord = true;
$model->setAttributes($attributes);
$model->save() && $model->id=0;
}