[英]Symfony2 flush() entities
我对Symfony2 Controller上的$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++;
}
因此,要执行此循环,我的脚本将花费很长时间,因为我的表中有约40,000个车辆。
我想每个循环中的$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[]
数组中的每个实体都可以!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.