级联={“删除"} VS orphanRemoval=true VS ondelete=“级联" [英] cascade={"remove"} VS orphanRemoval=true VS ondelete="CASCADE

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

问题描述

我尝试收集有关以下方法的一些信息,以便在删除父实体时自动删除子实体.似乎最常见的方法是使用这三个注解之一:cascade={remove"} OR orphanRemoval=true OR ondelete=CASCADE".

I tried to gather some information about the following way to delete automatically child entity when a parent entity is deleted. Seems that the most common way is to use one those three annotation: cascade={"remove"} OR orphanRemoval=true OR ondelete="CASCADE".

我对第三个有点困惑:ondelete=CASCADE",因为学说官方文档中关于这个的解释非常稀少)如果有人可以,我很乐意确认我的以下信息我从我在网络上的研究和经验中收集和理解...

I am a bit confused about the third one: ondelete="CASCADE", as the explanation in doctrine official documentation about this one are very scarce) and I would love if someone could confirm me the following information I gathered and understand from my research on the net and experience...

它有什么作用?

cascade={remove"}==>当拥有方实体是时,反向方的实体被删除.即使您与其他拥有方实体处于 ManyToMany 中.

cascade={"remove"} ==> the entity on the inverse side is deleted when the owning side entity is. Even if you are in a ManyToMany with other owning side entity.

  • 应该用于集合(所以在 OneToManyManyToMany 关系中)
  • 在 ORM 中的实现

orphanRemoval=true==>当拥有方实体是并且它不再连接到任何其他拥有方实体时,反向侧的实体将被删除.(参考.学说official_doc

orphanRemoval=true ==> the entity on the inverse side is deleted when the owning side entity is AND it is not connected to any other owning side entity anymore. (ref. doctrine official_doc

  • 在 ORM 中的实现
  • 可以与OneToOneOneToManyManyToMany
  • 一起使用
  • implementation in the ORM
  • can be used with OneToOne, OneToMany or ManyToMany

onDelete="CASCADE"==>这会将 On Delete Cascade 添加到数据库中的外键列

onDelete="CASCADE" ==> this will add On Delete Cascade to the foreign key column in the database

  • 这个策略要正确使用有点棘手,但可以非常强大和快速.(参考.doctrine official_doc ...但还没有阅读更多解释)
  • ORM 需要做的工作更少(与前两种方式相比),因此应该有更好的性能.
  • This strategy is a bit tricky to get right but can be very powerful and fast. (ref. doctrine official_doc ... but haven't read more explanations)
  • ORM has to do less work (compared to the two previous ways of doing it) and therefore should have better performance.

其他信息

  • 所有这 3 种方法都是在双向关系实体上实现的(对吗???)
  • 使用 cascade={remove"} 完全绕过 onDelete=CASCADE 的任何外键.(参考.doctrine_official_doc)
  • all those 3 ways of doing are implemented on bidirectional relationship entities (right???)
  • using cascade={"remove"} completely by-passes any foreign key onDelete=CASCADE. (ref. doctrine_official_doc)
  • orphanRemovalcascade={remove"} 在反向实体类中定义.
  • ondelete="CASCADE" 在所有者实体中定义
  • 你也可以只写 @ORMJoinColumn(onDelete="CASCADE") 并让学说处理列名
  • orphanRemoval and cascade={"remove"} are defined in the inversed entity class.
  • ondelete="CASCADE" is defined in the owner entity
  • you can also just write @ORMJoinColumn(onDelete="CASCADE") and let doctrine handle the column names

cascade={remove"}

/**
* @OneToMany(targetEntity="Phonenumber", mappedBy="contact", cascade={"remove"})
*/
protected $Phonenumbers

orphanRemoval=true

/**
* @OneToMany(targetEntity="Phonenumber", mappedBy="contact", orphanRemoval=true)
*/
protected $Phonenumbers

onDelete="CASCADE"

/** 
* @ManyToOne(targetEntity="Contact", inversedBy="phonenumbers")
* @JoinColumn(name="contact_id", referencedColumnName="contact_id", onDelete="CASCADE")
*/ 
protected $contact; 

推荐答案

onDelete="CASCADE" 由数据库本身管理.cascade={"remove"} 由教义管理.

onDelete="CASCADE" is managed by the database itself. cascade={"remove"} is managed by doctrine.

onDelete="CASCADE" 速度更快,因为操作是在数据库级别执行的,而不是按准则执行.删除是由数据库服务器而不是 Doctrine 执行的.使用 cascade={"remove"} 原则必须管理实体本身,并将执行额外检查以查看它是否没有任何其他拥有实体.当不存在其他实体时,它将删除该实体.但这会产生开销.

onDelete="CASCADE" is faster because the operations are performed on database level instead by doctrine. The removing is performed by the database server and not Doctrine. With cascade={"remove"} doctrine has to manage the entity itself and will perform extra checks to see if it doesn't have any other owning entities. When no other exists it will delete the entity. But this creates overhead.

cascade={"remove"}

  • 当拥有方实体被删除时,反向方的实体被删除.即使您与其他拥有方的实体处于许多状态.否,如果实体归其他人所有.它不会被删除.
  • 应该用于集合(所以在 OneToMany 或 ManyToMany 关系中)
  • 在 ORM 中的实现

orphanRemoval="true"

  • 当拥有方实体是并且它不再连接到任何其他拥有方实体时,反向侧的实体将被删除.不完全是,这使得学说表现得好像它不归其他实体所有,因此将其删除.
  • 在 ORM 中的实现
  • 可以与 OneToOne、OnetoMany 或 ManyToMany 一起使用

onDelete="CASCADE"

  • 这会将 On Delete Cascade 添加到数据库中的外键列
  • 这个策略要正确使用有点棘手,但可以非常强大和快速.(这是学说官方教程的引述……但还没有看到更多的解释)
  • ORM 需要做的工作更少(与前两种方式相比),因此应该有更好的性能.

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

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