Yii Framework Forum

Multi Insert Rows


(Konapaz) #1

Hi

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

I found

https://code.google.com/p/yii/issues/detail?id=2659

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

Thanks


(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 :

http://www.yiiframework.com/news/72/yii-1-1-14-release-candidate-is-available/

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

Yii::app()->db->schema->commandBuilder;

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

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

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

));

$command->execute();

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

Thanks

//only for 1.1.14

$builder=Yii::app()->db->schema->commandBuilder;

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

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

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

));

$command->execute()


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

Yii::app()->dbb->createCommand()->insert(‘user’,$insert_array);