【问题标题】:Symfony2 - doctrine2 batch processingSymfony2 - 学说 2 批处理
【发布时间】:2011-12-16 22:03:43
【问题描述】:

我有以下情况:

我需要基于一对实体创建大量实体(实体C)

  • 实体 A (45)
  • 实体 B (700000+)
  • 实体 C (45 x 700000)
  • 实体 D

所以我决定做以下事情:

$AEntities = $em->getRepository('MyBundle:EntityA')->findAll();
$DEntity = $em->getRepository('MyBundle:EntityD')->findOneBy($params);

$iterableResult = $em->getRepository('MyBundle:EntityB')
                ->createQueryBuilder('b')
                ->getQuery()->iterate();
$batchSize = 50

while (($row = $iterableResult->next()) !== false) {
  foreach($AEntities as $AEntity) {
    $entity = new Entity\EntityC();
    $entity->setEntityD($DEntity);
    $entity->setEntityB($row[0]);
    $entity->setEntityA($AEntity);
    $em->persist($entity);
  }

  if(($i % $batchSize) == 0){
    $em->flush();
    $em->clear();
  }
  $em->detach($row[0]);
  $i++;
}

$em->flush();

我听从doctrine2-batch-processing的指示

但是当我执行$em->detach($row[0]); 并刷新时出现错误A new entity was found through the relationship...

我试过不使用$em->detach($row[0]);,但内存消耗这么高

我需要:就是释放每个Entity B的内存,用完后,但同时每次flush或按组而不是一个一个一个一个,清空所有Entity C

【问题讨论】:

  • 定期清除实体管理器时,无需分离实体。
  • 如果执行$batchSize=50,这个工作正常,但对我来说不是一个好数字
  • 这是因为你在刷新之前分离了你的实体,所以它被实体管理器视为一个新实体......

标签: symfony doctrine-orm batch-processing


【解决方案1】:

调用clear() on entity manager 会分离所有对象(默认情况下)。顺便说一句,您可以传递实体名称来分离给定类型的实体:

$em->clear('EntityB'); 
$em->clear('EntityC');

我认为您正在尝试分离已经分离的实体,因此它被视为新实体。

尝试删除clear() 电话。您也可以尝试删除detach() 调用并在选定实体上调用clear()

【讨论】:

  • 我收到[Doctrine\ORM\ORMException] EntityManager#clear($entityName) not yet implemented.
  • 好像是在Doctrine2的master分支中实现的:github.com/doctrine/doctrine2
  • 我已经安装了2.1.4是最后一个包含在Symfony 2.0.7
【解决方案2】:

您必须完全指定实体名称

$em->clear('Acme\MyBundle\Entity\EntityB');
$em->clear('Acme\MyBundle\Entity\EntityC');

【讨论】:

    猜你喜欢
    • 2012-03-11
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    • 2011-09-02
    • 1970-01-01
    相关资源
    最近更新 更多