Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
946 views
in Technique[技术] by (71.8m points)

symfony - Doctrine - A new entity was found through the relationship

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

CRITICAL: DoctrineORMORMInvalidArgumentException:

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...