学说 – 通过这种关系find了一个新的实体

自2周以来,我们在尝试刷新新元素时遇到了这个问题:

CRITICAL:Doctrine \ ORM \ ORMInvalidArgumentException:

通过关系“Comment#capture”发现了一个新的实体,该关系没有被configuration为级联实体的持久化操作

但是capture已经在数据库中了,我们正在通过findOneBy来获取它,所以如果我们逐级保存它,或者坚持它,我们得到一个

表约束违规:重复条目。

注释是在不同的捕获循环中创build的,新的和所有必填字段都被设置。

在所有的实体被findOne (以及所有有效的)持久化和/或获取的情况下,刷新仍然失败。

我一直在这个问题上,所以请帮助我

我有同样的问题,这是相同的EntityManager 。 我想插入一个与ManyToOne相关的对象。 我不想要一个cascade persist

例如:

 $category = $em->find("Category", 10); $product = new Product(); $product->setCategory($category) $em->persist($product); $em->flush(); 

这引发了同样的例外。

所以解决办法是:

 $category = $em->find("Category", 10); $product = new Product(); $product->setCategory($category) $em->merge($product); $em->flush(); 

在我的情况下,太早的电话

 $this->entityManager->clear(); 

造成了这个问题。 它也消失了,只是对最近的对象做了明确的表述

 $this->entityManager->clear($capture); 

我的答案是相关的话题,但不是非常相关的特定情况下,所以对于那些使用谷歌search我张贴这个,因为上面的答案没有帮助我。

在我的情况下,我有与批处理实体有相同的错误,有关系和关系被设置为相同的实体。

我犯了什么错误:

当我做$this->entityManager->clear(); 在处理一批实体时,我会得到这个错误,因为下一批实体会指向分离的相关实体。

什么地方出了错:

  1. 我不知道$this->entityManager->clear();$this->entityManager->detach($entity); 只分离了所有的存储实体。

  2. 我认为$this->entityManager->clear(); 也分离相关实体。

我应该做什么:

我应该迭代实体并逐个分离它们 – 这不会分离未来实体指向的相关实体。

我希望这可以帮助别人。

首先,你应该更好地照顾你的代码,我看到你的实体和控制器中有3个不同的缩进 – 这很难阅读,不符合Symfony2编码标准 。

您为控制器显示的代码不完整,我们不知道$this->activeCapture来自哪里。 里面有一个$people['capture'] ,其中包含我假设的一个Capture对象。 这个非常重要。

如果$people['capture']的Capture是从另一个EntityManager持久/取得的,而不是$this->entityManager (我们不知道它来自哪里),Doctrine2并不知道该对象已经存在。

您应该确保对所有这些操作使用Doctrine Entity Manager的相同实例(使用EM对象上的spl_object_hash来确保它们是相同的实例)。

你也可以告诉EntityManager如何处理Capture对象。

 // Refreshes the persistent state of an entity from the database $this->entityManager->refresh($captureEntity); // Or // Merges the state of a detached entity into the // persistence context of this EntityManager and returns the managed copy of the entity. $captureEntity = $this->entityManager->merge($captureEntity); 

如果这没有帮助,你应该提供更多的代码。

错误:'Comment#capture'没有被configuration为级联实体的持久化操作

问题:

 /** * @ORM\ManyToOne(targetEntity="Capture", inversedBy="comments") * @ORM\JoinColumn(name="capture_id", referencedColumnName="id",nullable=true) */ protected $capture; 

不configuration级联坚持

试试这个:

 /** * @ORM\ManyToOne(targetEntity="Capture", inversedBy="comments", cascade={"persist", "remove" }) * @ORM\JoinColumn(name="capture_id", referencedColumnName="id",nullable=true) */ protected $capture; 

试图添加新的实体时,我也得到了这个错误。

 A new entity was found through the relationship 'Application\Entity\User#chats' that was not configured to cascade persist operations for entity: ###. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). 

我的情况是,我试图保存实体,这不应该被保存。 实体关系被填充并试图保存( User在Many2Many聊天,但聊天是一个临时实体),但是有一些碰撞。

所以,如果我使用cascade={"persist"}我得到不想要的行为 – 垃圾实体被保存。 我的解决scheme是从任何保存实体中删除非保存实体:

 // User entity code public function removeFromChats(Chat $c = null){ if ($c and $this->chats->contains($c)) { $this->chats->removeElement($c); } } 

保存代码

 /* some code witch $chat entity */ $chat->addUser($user); // saving $user->removeFromChats($chat); $this->getEntityManager()->persist($user); $this->getEntityManager()->flush();