如何插入多行?

时间:2015-06-29 15:21:41

标签: php arrays activerecord yii transactions

我有一组介于10到200之间的行,我需要将其插入到数据库中。 Active Records此任务的速度很慢,我通过这种方式使用命令构建器:

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

$ordersInfoToSave$itineraryInfoToSave是具有以下结构的属性数组。

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

由于我使用的是Yii-1.1.3,我没有使用CommandBuilder的方法createMultipleInsertCommand

那么,如何使用ActiveRecords以及CommandBuilder插入多行?

1 个答案:

答案 0 :(得分:1)

您可以创建一个MySql Insert Command字符串作为

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

}
相关问题