Doctrine - 通过关系发现了一个新实体 [英] Doctrine - A new entity was found through the relationship
问题描述
两周以来,我们在尝试刷新新元素时遇到了这个问题:
since 2 weeks, we are having this problem while trying to flush new elements:
关键:DoctrineORMORMInvalidArgumentException:
CRITICAL: DoctrineORMORMInvalidArgumentException:
通过关系 'Comment#capture' 发现了一个新实体,该实体未配置为对实体进行级联持久化操作
A new entity was found through the relationship 'Comment#capture' that was not configured to cascade persist operations for entity
但是capture
已经在数据库中了,我们通过findOneBy
获取它,所以如果我们级联持久化它,或者持久化它,我们得到一个
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.
所有实体都被持久化和/或通过 findOne
(并且都是有效的)获得,刷新仍然失败.
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
推荐答案
我遇到了同样的问题,而且是同一个 EntityManager
.我想插入一个与ManyToOne
相关的对象.而且我不想要 cascade
persist
.
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
.
示例:
$category = $em->find("Category", 10);
$product = new Product();
$product->setCategory($category)
$em->persist($product);
$em->flush();
这对我来说抛出了同样的异常.
This throws the same exception for me.
所以解决方案是:
$category = $em->find("Category", 10);
$product = new Product();
$product->setCategory($category)
$em->merge($product);
$em->flush();
这篇关于Doctrine - 通过关系发现了一个新实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!