Multi Insert Rows

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

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 :

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.

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

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

Thank u for replying kostas

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

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

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