Multi Insert Rows


Is there way to insert multi records of table using one query?

I found

but I want to ensure that (I cannot find insertMultiple method of CDbCommand in yii version 1.13)


You can do it by hand. INSERT INTO "XXX"(ccc,yyyy,uuu) VALUES (134,134,321,),(sdfd,8674,erfd), … collecting data in your model attributes aray from your form.

I cannot see another way to do it, I use personally foreach(blabla as $index=>my_model){ … my_model->save();}

version 1.14 include this method :

So You can wait for it or just take some code from there.

Hi Zugluk

I know that, I prefer a more robust and Yii way

Also I want use prepare statement to seperate query from data and preventing SQL injections

The second approach is very slow for many records, so one query is preferable in this issue.

In any case thanks! :)

oh! at last! Also we could wait for the Yii stable ver 1.1.14,

//only for 1.1.14


$command=$builder->createMultipleInsertCommand('tbl_post', array(

  array('title' => 'record 1', 'text' => 'text1'),

  array('title' => 'record 2', 'text' => 'text2'),



so you can replace kernel CDbCommandBuilder.php file from yiiframework/db/schema ver 1.1.14 to your yii core 1.1.13 (warning! -with your own risk!-) or extends the CDbCommandBuilder class adding the createMultipleInsertCommand and composeMultipleInsertCommand method

Thank u for replying kostas

I want raw query don’t want yii cdb commands .plz if u know reply fast . :(


//only for 1.1.14


$command=$builder->createMultipleInsertCommand(‘tbl_post’, array(

array(‘title’ => ‘record 1’, ‘text’ => ‘text1’),

array(‘title’ => ‘record 2’, ‘text’ => ‘text2’),



I can’t able to insert this array

$insert_array = array ( 
                                  [id] => 1 
                                  [user_type] => 1   
                                  [email_id] => 
                                  [password] => ****
                                  [is_logged_in] => 1