insert multiple record without loop-createMultipleInsertCommand

If you are developing any big application or we say a Product or ERP then database operation is very very important thing. Yii is very good for CRUD operation. It use save() function for inserting data in database. But when we deal with multiple data to insert at a time then save method and loop is not a good solution. It is very time consuming which degrade performance of application.

So Yii provides createMultipleInsertCommand method. This method could be used to achieve better performance during insertion of the large amount of data into the database tables.

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

So for this method we have to just create data array using loop. Then just execute the command.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s