Doctrine 2.0 与 2.1 级联删除 OneToMany

发布于 2024-12-10 11:27:54 字数 1224 浏览 0 评论 0原文

您好,我在尝试级联删除 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

前事休说 2024-12-17 11:27:54

尝试在您的 Company 类的 addresses 属性上使用它

@OneToMany(targetEntity="Address",mappedBy="company", 
cascade={"persist"}, orphanRemoval=true)

Try using this on the addresses attribute of your Company Class

@OneToMany(targetEntity="Address",mappedBy="company", 
cascade={"persist"}, orphanRemoval=true)
开始看清了 2024-12-17 11:27:54

我遇到了同样的问题...关系已添加或更新,但未删除,即使我有级联:[坚持,删除]。

我发现我不需要“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!

街角迷惘 2024-12-17 11:27:54

我遇到了同样的问题,我已经用该代码解决了他:

$em->remove($object);
$em->flush();
$em->remove($user);
$em->flush();

也许您可以在您的公司上使用 findAll 来获取地址,并使用 foreach 删除它 就像这样:

// Return all the addresses of the company
$addresses = $em->getRepository(...)->findAllAddressesByCompany($company);
$em->remove($company);
foreach ($address in $addresses)
{
    $em->remove($address);
}

这不是一个很好的方法,但目前,这就是我发现的全部。

I have met the same problem and i have solved him with that code :

$em->remove($object);
$em->flush();
$em->remove($user);
$em->flush();

Maybe you can use a findAll on your company for the addresses and remove this with a foreach like that :

// Return all the addresses of the company
$addresses = $em->getRepository(...)->findAllAddressesByCompany($company);
$em->remove($company);
foreach ($address in $addresses)
{
    $em->remove($address);
}

That's not a very good method but for now, that's all I've found.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文