1
//test id name
2
insert into test (name) values ('zhangsan'), ('lisi');
3
$names = ['zhangsan', 'lisi']; $data = []; foreach ($names $k => $v) { $data[] = [$v]; } Yii::$app->db->createCommand()->batchInsert('test', ['name'], $data)->execute();
举例4
上例中就是向user表的name和age字段插入三条数据.
// INSERT 一次插入多行 $connection->createCommand()->batchInsert('user', ['name', 'age'], [ ['Tom', 30], ['Jane', 20], ['Linda', 25], ])->execute();
参数1: 表名;参数2: 数组,在insert时的字段;;参数3: 数组,字段的值。