Yii Framework Forum

Multi Insert Rows

(Konapaz) #1


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)


(Lacffy Duck) #2

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();}

(D Mirecki) #3

version 1.14 include this method :


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

(Konapaz) #4

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! :)

(Konapaz) #5

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

I want one mysql query please reply fast.....
(Shobanac) #6

Thank u for replying kostas

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

(Magendiranmagi) #7


//only for 1.1.14


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

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

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



(karthikeyansankar) #8

I can’t able to insert this array

$insert_array = array ( 
                                  [id] => 1 
                                  [user_type] => 1   
                                  [email_id] => fori@care.com 
                                  [password] => ****
                                  [is_logged_in] => 1