DDC-2598: Entity removal not persisted when orphanRemoval set to false #3261

Closed
opened 2026-01-22 14:16:53 +01:00 by admin · 2 comments
Owner

Originally created by @doctrinebot on GitHub (Aug 7, 2013).

Originally assigned to: @Ocramius on GitHub.

Jira issue originally created by user sparrow:

I have a User class related to UserRole class. The second one is in relations with other entities also.
When I remove a UserRole from collection like below the removal is not persisted to the database unless I specify the orphanRemoval to true (on its mapping).
This seems weird because this is not the case of a User removal but the related UserRole. I think it is because the UserRole is in relation to other Entities which makes doctrine think I might be necessary to keep it in database.

    /****
     * @ORM\OneToMany(targetEntity="UserRole", mappedBy="user", cascade={"persist", "remove"})
     */
    private $userRoles;

        $roles = $user->getUserRoles();
        $roles->removeElement($roles[0]);

//      $role = new UserRole();
//      $role->setUser($user);
//      $role->setRole($em->find('Application\Entity\Role', 2));
//      $roles->add($role);

        $em->flush();
Originally created by @doctrinebot on GitHub (Aug 7, 2013). Originally assigned to: @Ocramius on GitHub. Jira issue originally created by user sparrow: I have a User class related to UserRole class. The second one is in relations with other entities also. When I remove a UserRole from collection like below the removal is not persisted to the database unless I specify the orphanRemoval to true (on its mapping). This seems weird because this is not the case of a User removal but the related UserRole. I think it is because the UserRole is in relation to other Entities which makes doctrine think I might be necessary to keep it in database. ``` /**** * @ORM\OneToMany(targetEntity="UserRole", mappedBy="user", cascade={"persist", "remove"}) */ private $userRoles; $roles = $user->getUserRoles(); $roles->removeElement($roles[0]); // $role = new UserRole(); // $role->setUser($user); // $role->setRole($em->find('Application\Entity\Role', 2)); // $roles->add($role); $em->flush(); ```
admin added the Bug label 2026-01-22 14:16:53 +01:00
admin closed this issue 2026-01-22 14:16:53 +01:00
Author
Owner

@doctrinebot commented on GitHub (Aug 7, 2013):

Comment created by @ocramius:

Removing an entity from an association has nothing to do with deleting it.

That's exactly what orphan removal achieves instead.

Additionally, in your example, you are not updating the owning side of the association when dissociating users from roles

@doctrinebot commented on GitHub (Aug 7, 2013): Comment created by @ocramius: Removing an entity from an association has nothing to do with deleting it. That's exactly what orphan removal achieves instead. Additionally, in your example, you are not updating the owning side of the association when dissociating users from roles
Author
Owner

@doctrinebot commented on GitHub (Aug 7, 2013):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Aug 7, 2013): Issue was closed with resolution "Invalid"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3261