[PR #8081] [Docs] Make clear that calling remove() detaches the object #10797

Open
opened 2026-01-22 16:08:43 +01:00 by admin · 0 comments
Owner

Original Pull Request: https://github.com/doctrine/orm/pull/8081

State: closed
Merged: Yes


I changed a relationship from eager to lazy loading which broke the behavior of my application in regards to object removal. It was not clear for me that removing an object detaches it and subsequent calls like contains() in a OneToMany relationship, with the object scheduled for removal, will return false afterwards. This together with a lazy loaded relation might lead to unexpected behavior.

Example:

$event = $repo->find(1);

$user = $event->getUser();
$this->em->remove($event);

$contains = $user->getEvents()->contains($event); //false, (lazy loaded list)
$count = $user->getEvents()->count(); //1 (the removed one, but different object / hash)

In combination with the Symfony Entity Generator it gets more confusing, since it provides a useful removeEvent function in the user object. This checks with a contain() if the object is there and does nothing otherwise.

$event = $repo->find(1);

$user = $event->getUser();
$this->em->remove($event);
$user->removeEvent($event); //From the Symfony entity generator, does nothing in this case since the contains check fails

$count = count($user->getEvents());  //Still 1, containing the lazy-loaded event object

Feel free to change the text to make the problem more clear, thanks!

**Original Pull Request:** https://github.com/doctrine/orm/pull/8081 **State:** closed **Merged:** Yes --- I changed a relationship from eager to lazy loading which broke the behavior of my application in regards to object removal. It was not clear for me that removing an object detaches it and subsequent calls like contains() in a OneToMany relationship, with the object scheduled for removal, will return false afterwards. This together with a lazy loaded relation might lead to unexpected behavior. Example: ```php $event = $repo->find(1); $user = $event->getUser(); $this->em->remove($event); $contains = $user->getEvents()->contains($event); //false, (lazy loaded list) $count = $user->getEvents()->count(); //1 (the removed one, but different object / hash) ``` In combination with the Symfony Entity Generator it gets more confusing, since it provides a useful `removeEvent` function in the user object. This checks with a `contain()` if the object is there and does nothing otherwise. ```php $event = $repo->find(1); $user = $event->getUser(); $this->em->remove($event); $user->removeEvent($event); //From the Symfony entity generator, does nothing in this case since the contains check fails $count = count($user->getEvents()); //Still 1, containing the lazy-loaded event object ``` Feel free to change the text to make the problem more clear, thanks!
admin added the pull-request label 2026-01-22 16:08:43 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#10797