主义“通过关系找到一个新的实体”错误 [英] Doctrine "A new entity was found through the relationship" error
问题描述
我收到这个错误,我认为我不应该得到它。 >
我有一个 OneToMany双向关系如下:
Class Channel
{
/ **
* @ ORM\OneToMany(targetEntity =Step,mappedBy =channel,cascade = {all},orphanRemoval = true)
* @ ORM\OrderBy({sequence=ASC})
* /
protected $ steps;
}
类步骤
{
/ **
* @ ORM\ManyToOne(targetEntity =Channel,inversedBy =steps)
* /
protected $ channel;
}
一个渠道
可以有许多步骤
,拥有方是频道
。从Doctrine 2.4 升级到 2.5 后,我收到此错误:
Doctrine\ORM\ORMInvalidArgumentException:通过关系Company\MyBundle\Entity\Step#channel找到一个新实体
,
未配置为级联实体的持久性操作
为什么甚至从逆向找到新的关系?这是我的代码:
$ channel = new Channel();
$ step = new Step();
$ channel-> addStep($ step);
$ em-> persist($ channel);
$ em-> flush();
谢谢!
你是对的:教义只会改变自己的一面,但你错了:你的关系的一边是步骤
,而不是渠道
。
为什么要拥有一面?因为是具有外键的实体。即使是Doctrine文档对您说
拥有一方必须使用OneToOne,
ManyToOne或ManyToMany映射的inversedBy属性宣言。 inversedBy属性
包含反向关联字段的名称。
可能的解决方案:
-
尝试通过将
cascade = {all}
转换为级联操作步骤实体(你确定 是正确的选择吗?) -
:
$ channel = new Channel();
$ step = new Step();
$ channel-> addStep($ step);
$ em-> persist($ channel);
$ em->持续($ step);
$ em-> flush();
这里你可以阅读为什么这里提供的第二种方式也很好
First off I want to say I've read through all the docs and googled this plenty before posting this question. I know what that error means (un-persisted entity in a relationship)
I'm getting this error where I think I shouldn't be getting it.
I have a OneToMany Bi-Directional relationship as follow:
Class Channel
{
/**
* @ORM\OneToMany(targetEntity="Step", mappedBy="channel", cascade={"all"}, orphanRemoval=true)
* @ORM\OrderBy({"sequence" = "ASC"})
*/
protected $steps;
}
Class Step
{
/**
* @ORM\ManyToOne(targetEntity="Channel", inversedBy="steps")
*/
protected $channel;
}
One Channel
can have many Step
s and the owning side is Channel
. After I upgraded from Doctrine 2.4 to 2.5 I'm getting this error:
Doctrine\ORM\ORMInvalidArgumentException: A new entity was found through the relationship 'Company\MyBundle\Entity\Step#channel' that was not configured to cascade persist operations for entity
why is it even finding new relationships from the inverse side? Here's my code:
$channel = new Channel();
$step = new Step();
$channel->addStep($step);
$em->persist($channel);
$em->flush();
Thanks!
You're right: Doctrine looks only for changes into owning side but you're wrong: owning side of your relationship is Step
, not Channel
.
Why is step the owning side? Because is the entity that has foreign key. Even Doctrine documentation says to you
The owning side has to use the inversedBy attribute of the OneToOne, ManyToOne, or ManyToMany mapping declaration. The inversedBy attribute contains the name of the association-field on the inverse-side.
Possible solutions:
Try to invert cascade operations by putting
cascade={"all"}
into Step entity (are you sure that all is the correct choice?)Persist explicitly both entities:
$channel = new Channel(); $step = new Step(); $channel->addStep($step); $em->persist($channel); $em->persist($step); $em->flush();
here you can read why second way provided here is fine too
这篇关于主义“通过关系找到一个新的实体”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!