Insert multiple data yii2 - Help please

Hello, I must insert 20 rows with data like this:

$sql = "INSERT INTO sims (id, user_id, sim, valor, factura, tiposim, bodega, estado, created_at, updated_at) 


(null,'$user_id', '$sim',  '$valor',  '$factura', '$tiposim',  '$bodega', '$estado', '$created_at', '$updated_at'),

(null,'$user_id', '$sim1', '$valor1', '$factura', '$tiposim1', '$bodega', '$estado', '$created_at', '$updated_at'),

(null,'$user_id', '$sim2', '$valor2', '$factura', '$tiposim2', '$bodega', '$estado', '$created_at', '$updated_at'),

(null,'$user_id', '$sim3', '$valor3', '$factura', '$tiposim3', '$bodega', '$estado', '$created_at', '$updated_at'),

(null,'$user_id', '$sim4', '$valor4', '$factura', '$tiposim4', '$bodega', '$estado', '$created_at', '$updated_at'),

.... until 20 total..

$sim, $valor and $tiposim fields are differents for each row, another fields keep the same data. The user can to insert between 5 to 20 rows.

Can someone please give me some sample code or any suggestions?

Thanks in advanced

Hi domenor,

I believe you need to use Command::batchInsert():

Yii::app->db->createCommand()->batchInsert('user', ['name', 'age'], [

	['Tom', 30],

	['Jane', 20],

	['Linda', 25],


1 Like