2012-03-16 108 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)