教义:级联=“删除";vs orphanRemoval=true [英] Doctrine: cascade="remove" vs orphanRemoval=true

查看:20
本文介绍了教义:级联=“删除";vs orphanRemoval=true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以上两个选项有什么区别?什么时候最好选择每个选项?

What is the difference between the 2 options above? When is it preferable to choose each option?

推荐答案

它们的基本区别是:

当使用 orphanRemoval=true 选项时,Doctrine 做出假设实体是私有的,不会被其他人重用实体.如果您忽略此假设,您的实体将获得即使您将孤立实体分配给 Doctrine,也会被 Doctrine 删除另一个.

When using the orphanRemoval=true option Doctrine makes the assumption that the entities are privately owned and will NOT be reused by other entities. If you neglect this assumption your entities will get deleted by Doctrine even if you assigned the orphaned entity to another one.

假设您的 UserComment 具有 一对多 关系.如果您使用 cascade="remove",您可以从一个 UserComment 的引用>,然后将该 Comment 附加到另一个 User.当您持久保存它们时,它们将被正确保存.但是如果您使用的是 orphanRemoval=true,即使您将从一个 User 中删除给定的 Comment,然后附加到另一个用户,这个评论会在persist期间被删除,因为引用已经被删除了.

Say your User has one-to-many relation to Comment. If you are using cascade="remove", you can remove the reference for Comment from one User, and then attach that Comment to another User. When you persist them, they will be correctly saved. But if you are using orphanRemoval=true, even if you will remove given Comment from one User, and then attach to another User, this comment will be deleted during persist, because the reference has been deleted.

这篇关于教义:级联=“删除";vs orphanRemoval=true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆