What is the difference between the 2 options above? When is it preferable to choose each option?
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.

- 1,933
- 1
- 19
- 31

- 4,332
- 4
- 23
- 37
-
3@a2ad2d what does **"privately owned"** means? I know that orphanRemoval=true can by the way be used in ManyToMany Relationship – Alexis_D Dec 15 '14 at 22:38
-
@Aleqxs can you provide an example? – Serge Kvashnin Dec 17 '14 at 13:26
-
@a2ad2d Category Entity and Article Entity in a ManyToMany relationship. see asked question on this subject [here](http://stackoverflow.com/questions/27476333/what-does-the-entities-are-privately-owned-means-as-seen-in-doctrine-official?lq=1) and some of the test I did to delete in cascade [here](http://stackoverflow.com/questions/27506876/deleting-entities-in-cascade-not-working-in-manytomany-relation) – Alexis_D Dec 17 '14 at 13:31
-
@Aleqxs Sorry, but it seems that in case of ManyToMany, orphanRemoval doesn't make sense – Serge Kvashnin Dec 17 '14 at 14:00
-
@a2ad2d but it is written that it "works with Many-To-Many associations" in the [offical doctrine documentation](http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-associations.html#orphan-removal) This is why I wonder what exactly means _**"privatly own"**_ – Alexis_D Dec 17 '14 at 14:37
-
@Aleqxs ok, i think that in your [case](http://stackoverflow.com/q/27506876/2431515) **privately owned** means your *Category* and it's references to *Article* – Serge Kvashnin Dec 17 '14 at 17:07
-
And if you don't assign the `comment` to another `user`, will the `comment` still be deleted even if you don't specify `orphanRemoval=true`? – darkbluesun Feb 04 '15 at 04:57
-
@darkbluesun you can specify `onDelete="SET NULL"` if you want to save the comment correctly after user removing. See [this](http://stackoverflow.com/a/11691485/2431515) answer. – Serge Kvashnin Feb 07 '15 at 08:26