yii2一次插入多行数据

时间:2022-01-22 06:43:20
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;}