Imagine you're storing some user settings in a Settings
entity which is attached to a User
entity via a One-To-Many or One-To-One relation, the Settings
entity won't be shared amongst several users, nor will it be reused if it's referenced user gets removed, you could say that Settings
is privately owned by User
and would become an orphan if User
gets deleted, orphanRemoval=true
prevents that from happening by deleting Settings
when its "mother" User
entity gets removed.
Some other questions on SO that you might find interesting :
Concerning Many-To-Many relations, you can use orphanRemoval
on such relations and Doctrine will simply remove attached entities, even if they are still attached to other entities.
Note that even though this option will work on Many-To-Many relations, it is not officially documented for such relations :
I don't think there's any example of a privately owned entity on a Many-To-Many relation, or it wouldn't be Many-To-Many, but Doctrine just makes the assumption it is, and will proceed to remove attached entities anyway.