Yii Framework Forum: Multi Insert Rows - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Multi Insert Rows in one query by Api of Yii Rate Topic: -----

#1 User is offline   Kostas Apazidis (KonApaz) 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,332
  • Joined: 21-February 11
  • Location:Greece

Posted 28 July 2013 - 12:02 PM

Hi

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

I found
https://github.com/y...yii/issues/1604
https://code.google..../detail?id=2659

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

Thanks
Yii is the best php framework in the world!
It is also powerful and flexible for large scale websites
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#2 User is offline   Zugluk 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 205
  • Joined: 08-August 12

Posted 29 July 2013 - 02:55 AM

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

#3 User is offline   mirunho 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 170
  • Joined: 19-December 12
  • Location:Gdansk, Poland

Posted 29 July 2013 - 03:45 AM

version 1.14 include this method :
http://www.yiiframew...e-is-available/
So You can wait for it or just take some code from there.
1

#4 User is offline   Kostas Apazidis (KonApaz) 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,332
  • Joined: 21-February 11
  • Location:Greece

Posted 29 July 2013 - 03:48 AM

View PostZugluk, on 29 July 2013 - 02:55 AM, said:

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


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! :)
Yii is the best php framework in the world!
It is also powerful and flexible for large scale websites
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#5 User is offline   Kostas Apazidis (KonApaz) 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,332
  • Joined: 21-February 11
  • Location:Greece

Posted 29 July 2013 - 04:05 AM

View Postmirunho, on 29 July 2013 - 03:45 AM, said:

version 1.14 include this method :
http://www.yiiframew...e-is-available/
So You can wait for it or just take some code from there.


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
Yii is the best php framework in the world!
It is also powerful and flexible for large scale websites
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users