2012-03-16 53 views
1

主义2.1版学说2.1 - 实体并不能被持续

我很坚持大量的对象,这就是为什么我有$this->entityManager->flush()后做$this->entityManager->clear(),但它会导致一个众所周知的错误:

Exception: "A new entity was found through the relationship 'Entities\A#B' that was not configured to cascade persist operations for entity: Entities\[email protected] Explicitly persist the new entity or configure cascading persist operations on the relationship. If you cannot find out which entity causes the problem implement 'Entities\B#__toString()' to get a clue."

它适用于第一次冲洗,但它不适用于所有其他冲洗。当我评论$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()函数修复这个问题,但我不想这样做,除非有管理一个更好的办法内存

+0

看起来你正在改变关联中的实体,但在关联的反面。所以当你试图坚持自己的一面,因为没有'CASCADE:persist'集合,它会抛出错误。 – 2012-03-19 03:32:11

回答

0

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

什么帮助对我来说是清除仅是越来越插入大量的实体(> 500.000),留下实体的关联对象的其余部分在内存

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

这虽然afaik只适用于从未版本的学说(> 2.2.0)和symfony2(> 2.1)