Doctrine 2.0 与 2.1 级联删除 OneToMany
您好,我在尝试级联删除 OneToMany 关系中的实体时遇到问题。 经过几个小时的调试后,我尝试将学说从最新的 2.1.2 降级到 2.0.2,它突然开始工作。
想象两个实体 Company 和 Address 的关系为 1:N。
/**
* @Entity
*/
class Company extends Entity
{
/**
* @var integer
* @id @Column(type="integer")
* @generatedValue
*/
private $id;
/**
* @var Collection
* @OneToMany(targetEntity="Address",mappedBy="company", cascade={"persist","remove"})
*/
private $addresses;
}
/**
* @Entity
*/
class Address extends Entity
{
/**
* @var integer
* @id @Column(type="integer")
* @generatedValue
*/
private $id;
/**
* @var Company
* @ManyToOne(targetEntity="Company", inversedBy="addresses")
* @JoinColumn(name="company_id", referencedColumnName="id",nullable=false)
*/
private $company;
}
当我尝试删除实体公司时,我希望分配的地址也将被删除。
$em->remove($company);
$em->flush();
在原则 2.1.2 中,未执行地址删除,因此完整性约束失败。在 2.0.2 版本中它可以完美运行。奇怪的是,如果我使用 EntityAudit 扩展 https://github.com/simplethings/EntityAudit LogRevisionListener 正在正确地版本控制解决了条令 2.1.2 中的实体(将它们设置为 revtype = DEL)(当然也在 2.0.2 中),但 UnitOfWork 并未删除它。
2.0.2 和 2.1.2 中处理级联删除的方式有什么区别吗?
非常感谢
Hello I have problem when trying to cascade remove entities in OneToMany relations.
After a few hours of debugging I tried to downgrade the doctrine from the latest 2.1.2 to 2.0.2 and It suddenly starts working.
Imagin two entities Company and Address in relation 1:N.
/**
* @Entity
*/
class Company extends Entity
{
/**
* @var integer
* @id @Column(type="integer")
* @generatedValue
*/
private $id;
/**
* @var Collection
* @OneToMany(targetEntity="Address",mappedBy="company", cascade={"persist","remove"})
*/
private $addresses;
}
/**
* @Entity
*/
class Address extends Entity
{
/**
* @var integer
* @id @Column(type="integer")
* @generatedValue
*/
private $id;
/**
* @var Company
* @ManyToOne(targetEntity="Company", inversedBy="addresses")
* @JoinColumn(name="company_id", referencedColumnName="id",nullable=false)
*/
private $company;
}
when I try to remove the entity Company, I would like the assigned addresses will be removed as well.
$em->remove($company);
$em->flush();
In doctrine 2.1.2 the deletion of addresses is not performed so the integrity constraint fails. In version 2.0.2 there it works perfectly. Wierd thing on it is, if I use EntityAudit extension https://github.com/simplethings/EntityAudit the LogRevisionListener is corretly versioning the addresses entities (set them revtype = DEL) in doctrine 2.1.2 (of course in 2.0.2 as well) but the UnitOfWork is not removing it.
Is there any difference how to handle cascade removing in 2.0.2 and in 2.1.2?
Thank you very much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试在您的
Company
类的addresses
属性上使用它Try using this on the
addresses
attribute of yourCompany
Class我遇到了同样的问题...关系已添加或更新,但未删除,即使我有级联:[坚持,删除]。
我发现我不需要“cascade”中的“remove”属性,但我必须添加orphanRemoval: true。
我快要疯了,你让我很开心!
I had the same problem... Relations were added or updated, but not deleted, even if I had cascade: [persist, remove].
I found out that I didn't need the "remove" attribute in "cascade", but I had to add the orphanRemoval: true.
I was going crazy, you made my day!
我遇到了同样的问题,我已经用该代码解决了他:
也许您可以在您的公司上使用 findAll 来获取地址,并使用 foreach 删除它 就像这样:
这不是一个很好的方法,但目前,这就是我发现的全部。
I have met the same problem and i have solved him with that code :
Maybe you can use a
findAll
on yourcompany
for the addresses and remove this with aforeach
like that :That's not a very good method but for now, that's all I've found.