第2.1条原则-实体不能持久化


doctrine 2.1 - entity cannot be persisted

条令2.1版

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

异常:"通过关系找到了一个新实体未配置为级联持久操作的"实体''A#B"实体:实体''B@00000000550760cc00000000b0edf71c。明确地持久化新实体或在上配置级联持久化操作关系。如果您无法找出导致问题实现"实体''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()函数修复了这个问题,但我不想这样做,除非有更好的方法来管理内存

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

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

尽管afaik只适用于条令(>2.2.0)和符号2(>2.1)的never版本

缺少的是在再次获取$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++;