本文介绍了关联实体未合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个这样的关联实体:
I have 2 associated entities like this:
class Solicitation {
<some fields>
/**
* @var \User
*
* @ORM\ManyToOne(targetEntity="User", fetch="EAGER", inversedBy="solicitation")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_user", referencedColumnName="id_user", nullable=false)
* })
* @OrderBy({"nome" = "ASC"})
*/
private $user;
<more fields>
}
我不想级联操作。问题是当我尝试在持续征集之前合并现有的用户,如下所示:
I don't want to cascade operations. The problem is when I try to merge an existing User before persisting Solicitation, like this:
$em = $this->getDoctrine()->getManager();
if (!(\Doctrine\ORM\UnitOfWork::STATE_MANAGED === $em->getUnitOfWork()->getEntityState($solicitation->getUser()))) {
$em->merge($solicitation->getUser());
}
$em->persist($solicitation);
...不会将User UnitOfWork状态更改为MANAGED。我还是分离,我收到和保存错误。
...it won't change User UnitOfWork state to "MANAGED". I`ts still "DETACHED" and I receive and error on saving.
推荐答案
我花了一整天找到
$em->merge($solicitation->getUser())
不会更改原始实体,它返回一个被盗的实体。所以正确的是:
doesn't change the original entity, it returns a menaged entity. So the correct is:
$solicitation->setUser($em->merge($solicitation->getUser()));
然后持续征求意见。做这个问题,以防其他人需要这个。
then persist solicitation. Made this question in case someone else needs this.
这篇关于关联实体未合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!