Doctrine: cascade="remove" vs orphanRemoval=true

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

34294 次浏览

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 Comment1 has Comment2 relation to Comment3. If you are using cascade="remove", you can remove the reference for Comment3 from one Comment1, and then attach that Comment3 to another Comment1. When you persist them, they will be correctly saved. But if you are using orphanRemoval=true, even if you will remove given Comment3 from one Comment1, and then attach to another Comment1, this comment will be deleted during persist, because the reference has been deleted.