在Symfony 1.4和Doctrine中插入带有一个查询的多行


Insert multiple rows with one query in Symfony 1.4 and Doctrine

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

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

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

我们该怎么做?

将每条记录添加到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();