Doctrine version 2.1
i am persisting a lot of objects, that is why I have to do $this->entityManager->clear() after $this->entityManager->flush(), however it causes a well known error:
Exception: “A new entity was found through the relationship
‘Entities\A#B’ that was not configured to cascade persist operations
for entity: Entities\B@00000000550760cc00000000b0edf71c. 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.”
It works for the first flush, but it does not work for all the others. When I comment $this->entityManager->clear();
Here is the code sample:
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++;
I will repeat that commenting out clear() function fixes the issue but i do not want to do that unless there is a better way to manage memory
What was missing is persist on $B after fetching it again.