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"
.
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...
What does it do?
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.
OneToMany
or ManyToMany
relationship)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
OneToOne
, OneToMany
or ManyToMany
onDelete="CASCADE"
==> this will add On Delete Cascade to the foreign key column in the database
other information
cascade={"remove"}
completely by-passes any foreign key onDelete=CASCADE. (ref. doctrine_official_doc)orphanRemoval
and cascade={"remove"}
are defined in the inversed entity class.ondelete="CASCADE"
is defined in the owner entity@ORM\JoinColumn(onDelete="CASCADE")
and let doctrine handle the column namescascade={"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;
If orphanRemoval is set to true, the line item entity will be deleted when the line item is removed from the order. The orphanRemoval attribute in @OneToMany and @oneToOne takes a Boolean value and is by default false.
Use the ON DELETE CASCADE option to specify whether you want rows deleted in a child table when corresponding rows are deleted in the parent table. If you do not specify cascading deletes, the default behavior of the database server prevents you from deleting data in a table if other tables reference it.
If an entity is removed from a @OneToMany collection or an associated entity is dereferenced from a @OneToOne association, this associated entity can be marked for deletion if orphanRemoval is set to true.
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"}
orphanRemoval="true"
onDelete="CASCADE"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With