我有一个关于Symfony2控制器上的$em->flush()
的问题。
$v = $em->getRepository("ApplicationAdminBundle:VehiculeCompute")->findAll();
foreach ($v as $vehicule) {
[...]
$somme = {"compute before"};
$veh = $em->getRepository("ApplicationAdminBundle:VehiculeCompute")->find($vehicule->getIdvehicules());
$veh->setTaxeadditionnelle($somme);
$em->flush();
$total++;
}
因此,要执行这个循环,我的脚本需要很长时间,因为我的表中有大约40000个vehicules。
我想每个循环中的$em->flush()
不是必需的。。。
$v = $em->getRepository("ApplicationAdminBundle:VehiculeCompute")->findAll();
$k = 0;
foreach ($v as $vehicule) {
[...]
$somme = {"compute before"};
$veh[$k] = $em->getRepository("ApplicationAdminBundle:VehiculeCompute")->find($vehicule->getIdvehicules());
$veh[$k]->setTaxeadditionnelle($somme);
$total++;
}
$em->flush(); // Flush all of vehicule update ?!
unset($veh);
这个版本可以工作吗?感谢
与其使用实体管理器对大量数据执行相同的操作,不如处理类似的查询生成器更新
$em
->getRepository('Foo')
->createQueryBuilder()
->update()
->field('bar')->set('value')
->getQuery()
->execute();
否则,在大数组上使用flush()时要小心。我在使用这种方法时遇到了一些问题,通过使用array_chunk和刷新100个或更少的项目
相反,您可以按如下方式每20次刷新一次。这样做要快得多。
$v = $em->getRepository("ApplicationAdminBundle:VehiculeCompute")->findAll();
$index = 0;
foreach ($v as $vehicule) {
[...]
$somme = {"compute before"};
$veh = $em->getRepository("ApplicationAdminBundle:VehiculeCompute")->
find($vehicule->getIdvehicules());
$veh->setTaxeadditionnelle($somme);
$em->persist($veh);
if($index%20==0)
$em->flush();
$index++;
}
$em->flush();
谢谢大家!循环外有一个$em->flush()
,每个实体都在$veh[]
数组中,ALL就可以了!