mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Documentation on "orphanRemoval" is ambiguous (if not wrong) #7273
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @janopae on GitHub (Dec 6, 2023).
Bug Report
Summary
The documentation about orphanRemovel currently states the following:
Is is not clear however what "privately owned" means in that context. Further, it is not clear whether "entity" in this case refers to an entity class or to an instance of an entity class.
In the paragraph before, the term "entity" seems to be referring to a single instance of an entity class. Therefore, "entities will get deleted by Doctrine even if you assigned the orphaned entity to another one" would mean that assigning a child entity to a new parent would also make doctrine remove them as an orphan.
Impact
When I was reading the docs, I thought
orphanRemovalwould be unsuitable for my use case: I have a parent and a child class, the parent being the "aggregate root" (in terms of Domain Driven Design that means that there is no repository for the child, just for the parent). I wanted children to be removed when they have no parent, so you could remove them on the parent class without having to call the entity manager. However, child classes should also be able to move to another parent.After some hours of searching for alternatives, I decided to give it a shot anyway, and to my surprise it worked as desired.
The warning might prevent many from using
orphanRemovalin the first place, implying it only served a niche purpose, while it actually would exactly solve their problem.How to reproduce
Consider the following example entities:
According to the documentation, you'd expect the second of the following test cases to fail:
However, it succeeds as desired.
I'd argue that this is good behaviour and should be kept – the documentation should be adjusted. What do you think?
I thought about creating a PR for the docs btw., but I think I don't yet know enough about how this works with ManyToMany relationships, and how Doctrine behaves if you have multiple relationships to a child with orphanRemoval=true.
@jwandrews commented on GitHub (Jan 31, 2024):
came across this while looking for a different issue i have with orphan removal.
if i understand the documentation correctly, the original child entity is deleted with orphan removal being true. your test assertions aren't necessarily testing for equality between the 2 children. i think you may find that during a test run, if you checked the ids of the children before and after 'moving' between parents, they would be different since doctrine will remove
$parent1's child and create a new child on$parent2vs simply updating the parent reference on the child@janopae commented on GitHub (Feb 2, 2024):
You could be right. Thanks for sharing this observation!
@jwandrews commented on GitHub (Feb 2, 2024):
FWIW, the issue I was/am having with
orphanRemoval, when i found this issue, is that it apparently does not work as expected onOneToOnerelationships. I was under the assumption it would. It does however work as the documentation states onManyToOnerelationships.@janopae commented on GitHub (Feb 2, 2024):
What exactly is wrong with OneToOne relationships? Maybe you can create a separate issue explaining this?