Woj*_*lik 9 optimization doctrine symfony
使用doctrine,symfony2更新数据库中的多个记录的最佳方法是什么?
我收到了一些记录ID,我必须更新.
我想为每个记录分配从接收到的数组到列show_order的索引.所以,如果我收到数组$ array = array(22,1,5,10),那么我想做
$i = 0;
foreach($array as $a) {
$record = $this->getDoctrine->getRepository('AcmeBundle:SomeEntity')->findOneById($a);
if ($record != null) $record->setOrder($i++);
}
$this->getDoctrine()->getEntityManager()->flush();
Run Code Online (Sandbox Code Playgroud)
但这是可怕的方式,因为对于每个记录我做一个SELECT,所以查询数量是O(n).
怎么做得更好?
就像是...
foreach ($repo->findById($ids) as $obj) {
$obj->setOrder(array_search($obj->getId(), $ids));
}
$em->flush();
Run Code Online (Sandbox Code Playgroud)