Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-2598

Entity removal not persisted when orphanRemoval set to false

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Invalid
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Security Level: All
    • Labels:
      None

      Description

      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();
      

        Activity

        Hide
        Marco Pivetta added a comment -

        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

        Show
        Marco Pivetta added a comment - 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

          People

          • Assignee:
            Marco Pivetta
            Reporter:
            Przemyslaw Wrobel
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: