[英]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.