Symfony2 flush()实体
我在Symfony2控制器上有关于$em->flush()
的问题。 Symfony2 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);
这个版本可以正常工作? 谢谢。
相反,您可以按照以下方式每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();
没有persit(),因为我只是更新' - > find()'实体 – RudySkate 2014-08-29 09:29:21
而不是使用实体管理器上的大量数据执行相同的操作它建议用户在处理像
$em
->getRepository('Foo')
->createQueryBuilder()
->update()
->field('bar')->set('value')
->getQuery()
->execute();
否则)查询生成器更新使用冲洗时要小心(大阵。我有这个用法的几个问题,通过使用array_chunk和冲洗100个或更少的项目解决
Thx。我用queryBuilder实例知道这个过程。我会试着用^^ – RudySkate 2014-08-29 09:35:05
看看我的编辑;) – 2014-08-29 13:08:06
谢谢大家! 在$veh[]
阵列外面有一个$em->flush()
,并且每个实体都是OK!
我最初的想法是,这是一个ORM不合适的场合。您应该使用Doctrine查询生成器来直接对话DBAL层。 – lonesomeday 2014-08-29 08:49:22
有关于Doctrine文档中批处理的一些信息 - http://docs.doctrine-project.org/zh/2.0.x/reference/batch-processing.html#bulk-inserts – qooplmao 2014-08-29 08:54:15
@Prisoner'persist'是为了坚持新实体在更新已经存在的实体时,不应该使用它。 – 2014-08-29 09:18:14