2014-01-22 59 views
1

我想在循环中保存很多CActiveRecord模型对象。 我有这样的事情:Yii:在一个查询中保存多条记录

foreach ($array_of_items as $item) { 

    $values = array(
     "title" => $item->title, 
     "content" => $item->content, 
    ); 

    $object = new MyModel; 
    $object->attributes = $values; 
    $object->save(); 

} 

在我而言,这造成约400的CActiveRecord对象。保存过程非常缓慢,因为每个save()查询数据库。

有没有办法一次保存所有这些对象? 类似于:

$objects = array(); 

foreach ($array_of_items as $item) { 

    $values = array(
     "title" => $item->title, 
     "content" => $item->content, 
    ); 

    $object = new MyModel; 
    $object->attributes = $values; 
    $objects[] = $object; 
} 

save_all_objects($objects); 

我在主题上找不到任何东西。任何人?

+0

不,从我花在上面的时间量来看,Yii没有什么可做的,你现在正在做的这种方式应该是唯一的选择 - 让我感兴趣的是自己知道是否有替换。 – Rohan

+1

我想你可以在[这个问题]的接受答案中找到你的解决方案(http://stackoverflow.com/questions/18518001/batch-insert-in-yii)。 – ndgraef

+0

好吧,事实证明,我真正需要的是使用交易。在foreach循环中保存400个模型:25秒。在beginTransaction&commit中包装foreach循环:0.36秒。 –

回答

4

可以validate()模型,如果它是确定可以追加这么一个SQL文本插入,

和你的循环后,只需使用数据库并执行准备好的文本

$sql = ''; 
if($object->validate()) 
{ 
    $sql .= ',("' . $object->attr1 . '")'// append to script,(you get the idea, you need to also make a correct values) 
} 

... 

if(!empty($sql)) 
{ 
    $sql = 'INSERT INTO table (attr1) Values' . $sql;// make complete script 
    // execute that command 
} 
0

对于插入多行,将此代码放在components文件夹下的GeneralRepository.php文件名下。

<?php 
class GeneralRepository 
{ 
    /** 
    * Creates and executes an INSERT SQL statement for several rows. 
    * 
    * Usage: 
    * $rows = array(
    *  array('id' => 1, 'name' => 'John'), 
    *  array('id' => 2, 'name' => 'Mark') 
    *); 
    * GeneralRepository::insertSeveral(User::model()->tableName(), $rows); 
    * 
    * @param string $table the table that new rows will be inserted into. 
    * @param array $array_columns the array of column datas array(array(name=>value,...),...) to be inserted into the table. 
    * @return integer number of rows affected by the execution. 
    */ 
    public static function insertSeveral($table, $array_columns) 
    { 
     $connection = Yii::app()->db; 
     $sql = ''; 
     $params = array(); 
     $i = 0; 
     foreach ($array_columns as $columns) { 
      $names = array(); 
      $placeholders = array(); 
      foreach ($columns as $name => $value) { 
       if (!$i) { 
        $names[] = $connection->quoteColumnName($name); 
       } 
       if ($value instanceof CDbExpression) { 
        $placeholders[] = $value->expression; 
        foreach ($value->params as $n => $v) 
         $params[$n] = $v; 
       } else { 
        $placeholders[] = ':' . $name . $i; 
        $params[':' . $name . $i] = $value; 
       } 
      } 
      if (!$i) { 
       $sql = 'INSERT INTO ' . $connection->quoteTableName($table) 
       . ' (' . implode(', ', $names) . ') VALUES (' 
       . implode(', ', $placeholders) . ')'; 
      } else { 
       $sql .= ',(' . implode(', ', $placeholders) . ')'; 
      } 
      $i++; 
     } 
     $command = Yii::app()->db->createCommand($sql); 
     return $command->execute($params); 
    } 
} 

和使用的任何地方:

$rows = array(
    array('id' => 1, 'name' => 'John'), 
    array('id' => 2, 'name' => 'Mark') 
); 
GeneralRepository::insertSeveral(User::model()->tableName(), $rows); 
0

由于v1.1.14,createMultipleInsertCommand()CDbCommandBuilder类的方法是可行的。

相关问题