Doctrine - A new entity was found through the relationship

isundil picture isundil · Aug 13, 2013 · Viewed 84.5k times · Source

since 2 weeks, we are having this problem while trying to flush new elements:

CRITICAL: Doctrine\ORM\ORMInvalidArgumentException:

A new entity was found through the relationship 'Comment#capture' that was not configured to cascade persist operations for entity

But the capture is already in the database, and we are getting it by a findOneBy, so if we cascade persist it, or persist it, we get a

Table constraint violation: duplicate entry.

The comments are created in a loop with differents captures, with a new, and all required field are set.

With all of the entities persisted and / or got by a findOne (and all valid), the flush still fails.

I'm on this issue since a while, so please help me

Answer

Mirza Selimovic picture Mirza Selimovic · Dec 11, 2013

I had the same problem and it was the same EntityManager. I wanted to insert an object related ManyToOne. And I don't want a cascade persist.

Example :

$category = $em->find("Category", 10);

$product = new Product();
$product->setCategory($category)

$em->persist($product);
$em->flush();

This throws the same exception for me.

So the solution is :

$category = $em->find("Category", 10);

$product = new Product();
$product->setCategory($category)

$em->merge($product);
$em->flush();