Max Maximov
Max Maximov

Reputation: 123

How to insert multiple rows?

I have a set of rows between 10 to 200, which i need to insert into Database. Active Records is slow for this task, i am using command builder by this way:

        $builder = Yii::app()->db->schema->commandBuilder;
        $command=$builder->createMultipleInsertCommand('AviaOrders',$ordersInfoToSave );
        $command=$builder->createMultipleInsertCommand('ItineraryInfo',$itineraryInfoToSave );
        $command->execute();

Where $ordersInfoToSave and $itineraryInfoToSave are arrays of attributes with following structure.

 array(array(name=>value,...),array(name=>value,...),...)

Since i am using Yii-1.1.3, i have not CommandBuilder's method createMultipleInsertCommand.

So, how to insert multiple rows, using ActiveRecords as well as CommandBuilder?

Upvotes: 0

Views: 173

Answers (1)

Double H
Double H

Reputation: 4160

You can create a string of MySql Insert Command as

$ordersInfoToSave='';

foreach ($records as $record)
$ordersInfoSave.="('{$record['attribute1vlaue']}','{$record['attribute2vlaue']}'),";

if($ordersInfoSave !== NULL){
$ordersInfoSave=substr($ordersInfoSave,0,-1);
$query='Insert Into AviaOrders (`attribute1`,`attribute2`) values '.$ordersInfoSave;


Yii::app()->db->createCommand($query)->execute();

}

Upvotes: 1

Related Questions