Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-1504

Cascade remove in OneToMany relation doesn't work

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Incomplete
    • Affects Version/s: 2.1.2
    • Fix Version/s: 2.2.3
    • Component/s: ORM
    • Security Level: All
    • Labels:
      None
    • Environment:
      Ubuntu 10.10, syfmony 2.0.4

      Description

      I have two entities :

      class Structure
      {
          // Id and other variables
      
          /**
           * @var ArrayCollection $employees liste des employés d'une structure
           *
           * @ORM\OneToMany(targetEntity="Uriae\EmployeeBundle\Entity\Employee", mappedBy="structure", cascade={"all"})
           */
          private $employees;
      }
      
      class Employee
      {
          // Id and other variables
      
          /**
           * @var integer $structure
           *
           * @ORM\ManyToOne(targetEntity="Uriae\StructureBundle\Entity\Structure",inversedBy="employees")
           */
          private $structure;
      }
      

      The problem is when I used tests and I wan't remove a Structure the Employees aren't removed.

      // Doesn't work
      $em->remove($structure);
      $em->flush();
      
      // Bad solution but it works
      $em->remove($structure);
      $em->flush();
      foreach ($employee as $employees)
      {
          $em->remove($employee);
      }
      $e->flush();
      

        Activity

        Hide
        Benjamin Eberlei added a comment -

        No feedback given.

        Show
        Benjamin Eberlei added a comment - No feedback given.
        Hide
        Benjamin Eberlei added a comment -

        1. orphanRemoval is not the right solution, it has different semantics. If you want this semantics the solution is ok.
        2. Has anyone tried updating to anything above 2.1.2?
        3. I cannot reproduce with a test-case against the current master or 2.1.5

        I need a better testcase. Attached is the one i am working with.

        Show
        Benjamin Eberlei added a comment - 1. orphanRemoval is not the right solution, it has different semantics. If you want this semantics the solution is ok. 2. Has anyone tried updating to anything above 2.1.2? 3. I cannot reproduce with a test-case against the current master or 2.1.5 I need a better testcase. Attached is the one i am working with.
        Hide
        Nurlan Turdaliev added a comment -

        If you add orphanRemoval="true" it works.
        But I think doctrine should somehow let the user know about this problem (throw exception, etc.)
        Maybe it worths making orphanRemoval=true when cascading=remove?

        Show
        Nurlan Turdaliev added a comment - If you add orphanRemoval="true" it works. But I think doctrine should somehow let the user know about this problem (throw exception, etc.) Maybe it worths making orphanRemoval=true when cascading=remove?
        Hide
        Benjamin Eberlei added a comment -

        Fixed formatting

        Show
        Benjamin Eberlei added a comment - Fixed formatting
        Hide
        Gaetan Rousseau added a comment -

        I know, it's me who answered. But the solution i've found is "ugly".

        Show
        Gaetan Rousseau added a comment - I know, it's me who answered. But the solution i've found is "ugly".
        Hide
        petrs added a comment -
        Show
        petrs added a comment - The same problem, described here http://stackoverflow.com/questions/7766999/doctrine-2-0-vs-2-1-cascade-remove-onetomany

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Gaetan Rousseau
          • Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: