【问题标题】:Insert multiple rows with one query in Symfony 1.4 and Doctrine在 Symfony 1.4 和 Doctrine 中用一个查询插入多行
【发布时间】:2015-03-25 10:01:45
【问题描述】:

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

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

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

我们该怎么做?

【问题讨论】:

  • 已经试过this
  • 非常感谢亲爱的参考,我去试试
  • 哇,谢谢亲爱的,效果很好...您可以添加您的评论作为答案,使其成为最佳答案!

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


【解决方案1】:

将每条记录添加到集合对象上的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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多