【问题标题】:doctrine 2.1 - entity cannot be persisted学说 2.1 - 实体不能被持久化
【发布时间】:2012-03-16 11:34:36
【问题描述】:

学说 2.1 版

我坚持了很多对象,这就是为什么我必须在$this->entityManager->flush() 之后执行$this->entityManager->clear(),但这会导致一个众所周知的错误:

异常:“通过关系发现了一个新实体 'Entities\A#B' 未配置为级联持久化操作 对于实体:实体\B@00000000550760cc00000000b0edf71c。明确地 持久化新实体或配置级联持久化操作 关系。如果您无法找出是哪个实体导致 问题实现 'Entities\B#__toString()' 以获得线索。”

它适用于第一次冲洗,但不适用于所有其他冲洗。当我评论$this->entityManager->clear();

这是代码示例:

  if ($flushCounter % 50 == 0) {
    $this->entityManager->flush();
    $this->entityManager->clear();
    //$this->entityManager->detach($B); <- with these two lines commented I tried to fix the error, but it did not work
    //$B = $this->entityManager->find(ENTITY_NAMESPACE . "\B", (int) $B_id);
  }
  $flushCounter++;

我会重申,注释掉 clear() 函数可以解决问题,但我不想这样做,除非有更好的方法来管理内存

【问题讨论】:

  • 您似乎正在更改关联中的实体,但在关联的反面。因此,当您尝试保留拥有方时,由于没有设置 CASCADE: persist,它会引发错误。

标签: php memory-management doctrine-orm


【解决方案1】:

对我有帮助的是只清除大量插入的实体 (> 500.000),将该实体的其余关联对象留在内存中

if ($repcount++ % 1000 == 0) {
    $em->flush();   
    $em->clear('Entity\Class\Using\Memory');
}

虽然 afaik 仅适用于从不版本的学说 (> 2.2.0) 和 symfony2 (>2.1)

【讨论】:

    【解决方案2】:

    再次获取 $B 后,丢失的内容仍然存在。

     if ($flushCounter % 50 == 0) {
        $this->entityManager->flush();
        $this->entityManager->clear();
        $B = $this->entityManager->find(ENTITY_NAMESPACE . "\B", (int) $B_id);
        $this->entityManager->persist($B);
      }
      $flushCounter++;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-08
      • 2012-01-09
      • 2014-11-27
      相关资源
      最近更新 更多