cascade = {“remove”} VS orphanRemoval = true VS ondelete =“CASCADE [英] cascade={"remove"} VS orphanRemoval=true VS ondelete="CASCADE

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

问题描述

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



我对有点混淆了第三个: ondelete =CASCADE ,作为教义官员的解释关于这一个的文件非常稀缺),如果有人可以确认我以下信息,我会很喜欢我从我的网络和经验研究中收集和了解...



它是什么



cascade = {remove}

==>当拥有方实体时,反面的实体被删除。

- 应该用于收集(所以在OneToMany或ManyToMany关系中)

- 在ORM中实现



orphanRemoval = true

==>当拥有方实体为AND且未连接时,反面的实体被删除再到任何其他拥有方的实体。 (参考 doctrine official_doc
- 在ORM中实现

- 可以与OneToOne,OnetoMany或ManyToMany一起使用



onDelete = CASCADE

==>这将添加到删除级联到数据库中的外键列

- 这个策略是有点棘手得到正确,但可以非常强大和快速(参考 doctrine official_doc ...但没有阅读更多的解释)

- ORM必须做更少的工作(与之前的两种方式相比),因此应该有更好的性能。



其他信息

- 所有这三种做法都是在双向关系实体上实现的( right? ?

- 使用cascade = {remove}完全绕过Delete = CASCADE上的任何外键。 (参考 doctrine_official_doc



如何在代码中使用示例




  • orphanRemoval and cascade = { remove}在反向实体类中定义。

  • ondelete =CASCADE在所有者实体

  • 中定义,您还可以写@ ORM\JoinColumn(onDelete =CASCADE)并且让教条处理列名称



cascade = {remove}

  / ** 
* @OneToMany(targetEntity =Phonenumber,mappedBy =contact,cascade = {remove})
* /
保护$ 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;
$ / pre>

解决方案

onDelete =CASCADE 由数据库本身管理。 cascade = {remove} 由学说管理。



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






cascade = {remove}




  • 当拥有方实体时,反面的实体被删除。即使你与其他拥有方实体有很多关系。不,如果实体由其他东西拥有。它不会被删除。

  • 应该用于收藏(所以在OneToMany或ManyToMany关系中)

  • ORM中的实现



orphanRemoval =true




  • 当拥有方实体为AND且不再与任何其他拥有方实体连接时,相反方面的实体将被删除。 不完全是这样,这使得教义的行为就像它不是由其他实体拥有,因此将其删除。

  • ORM

  • 可用于OneToOne,OnetoMany或ManyToMany



onDelete = CASCADE




  • 这将添加On Delete Cascade到外键列IN数据库

  • 这个策略是有点棘手的做法,但可以非常强大和快速。 (这是从教义官方教程的引用...但没有看到更多的解释)

  • ORM必须做更少的工作(与之前的两种方式相比),因此应该有更好的表现。


I tried to gather few information about those 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".

I am a bit confuse about the third one: ondelete="CASCADE", as 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...

WHAT IT DOES

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.
- should be used on collection (so in OneToMany or ManyToMany relationship)
- implementation in the ORM

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 - implementation in the ORM
- can be used with OneToOne, OnetoMany or ManyToMany

onDelete="CASCADE"
==> this will add On Delete Cascade to the foreign key column in the database
- 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 explainations)
- ORM has to do less work (compared to the two previous way of doing) and therefore should have better performance.

other information
- all those 3 way of doing are implemented on bidirectionnal relationnship entities (right???)
- using cascade={"remove"} completely by-passes any foreign key onDelete=CASCADE. (ref. doctrine_official_doc)

EXAMPLE ON HOW TO USE IT IN CODE

  • orphanRemoval and cascade={"remove"} are defined in the inversed entity class.
  • ondelete="CASCADE" is defined in the owner entity
  • you can also just write @ORM\JoinColumn(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" is managed by the database itself. cascade={"remove"} is managed by doctrine.

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"}

  • 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. No, if the entity is owned by something else. It won't be deleted.
  • should be used on collection (so in OneToMany or ManyToMany relationship)
  • implementation in the ORM

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. Not exactly, this makes doctrine behave like it is not owned by an other entity, and thus remove it.
  • implementation in the ORM
  • can be used with OneToOne, OnetoMany or ManyToMany

onDelete="CASCADE"

  • this will add On Delete Cascade to the foreign key column IN THE DATABASE
  • This strategy is a bit tricky to get right but can be very powerful and fast. (this is a quote from doctrine official tutorial... but haven't seen much more explaination)
  • ORM has to do less work (compared to the two previous way of doing) and therefore should have better performance.

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

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