在Symfony 1.4和Doctrine中使用一个查询插入多行

时间:2015-03-25 10:01:45

标签: php mysql doctrine-orm doctrine symfony-1.4

我有客户端集合,每个集合都可以包含许多客户端。这个PHP代码循环集合,以及每个集合中的客户端。并将客户端保存到数据库中。

foreach ($collections as $key => $collection) {
      foreach ($collection as $k => $client) {
                 $name = $client['name'];
                 //...
                 $clientObj = new Client();
                 $clientObj->setName($name);
                 //..
                 $clientObj->save();
      }
}

我想要做的是将每个集合分组到一个Mysql查询中,然后转到下一个集合。因为前面的代码每个客户端执行一个查询,而且为了提高性能,我们每个集合需要一个查询。

我们怎么能这样做?

1 个答案:

答案 0 :(得分:2)

将每条记录添加到集合对象上的Doctrine_Collection调用save()

 * Saves all records of this collection and processes the 
 * difference of the last snapshot and the current data

例如:

$collection = new Doctrine_Collection('client');
$collection->add($client1);
$collection->add($client2);
$collection->save();