Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine - A new entity was found through the relationship

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

like image 299
isundil Avatar asked Aug 13 '13 17:08

isundil


2 Answers

In my case a too early call of

$this->entityManager->clear(); 

caused the problem. It also disappeared by only doing a clear on the recent object, like

$this->entityManager->clear($capture); 
like image 28
ownking Avatar answered Oct 27 '22 00:10

ownking


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(); 
like image 140
Mirza Selimovic Avatar answered Oct 27 '22 00:10

Mirza Selimovic