尝试添加新实体时,我也收到此错误。
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
configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}).
我的情况是我尝试保存实体,不应保存该实体。实体关系被填充并试图保存(在Many2Many中User
具有Chat
,但是Chat是一个临时实体),但是存在一些冲突。
因此,如果我使用该程序,cascade={"persist"}
则会发生不良行为-保存垃圾箱实体。我的解决方案是从所有保存实体中删除非保存实体:
public function removeFromChats(Chat $c = null){
if ($c and $this->chats->contains($c)) {
$this->chats->removeElement($c);
}
}
保存代码
$chat->addUser($user);
$user->removeFromChats($chat);
$this->getEntityManager()->persist($user);
$this->getEntityManager()->flush();