Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine: cascade="remove" vs orphanRemoval=true

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

like image 208
iiirxs Avatar asked Aug 26 '14 20:08

iiirxs


People also ask

Is orphanRemoval true?

If orphanRemoval=true is specified the disconnected Address instance is automatically removed. This is useful for cleaning up dependent objects (e.g. Address ) that should not exist without a reference from an owner object (e.g. Employee ).

What is the use of orphanRemoval?

As stated earlier, its usage is to delete orphaned entities from the database. An entity that is no longer attached to its parent is the definition of being an orphan.


1 Answers

The basic difference between them is:

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.

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.

like image 140
Serge Kvashnin Avatar answered Oct 28 '22 01:10

Serge Kvashnin