Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-2382

Multiple relations between two same entities breaks cascade operations

    Details

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

      Description

      /**
       * @Entity
       */
      
      class User
      {
      /**
       * @Id
       * @Column(type="integer")
       * @GeneratedValue(strategy="AUTO")
       */
      
      protected $id;
      
      /**
       * @OneToMany(targetEntity="Post", cascade={"remove"}, mappedBy="author")
       */
      
       protected $posts;
      
      /**
       * @OneToOne(targetEntity="Post")
       */
      
       protected $lastPost;
      
      public function setLastPost( $post )
      {
          $this->lastPost = $post;
      }
      
        // ...skipped...
      
      }
      
      
      /**
       * @Entity
       */
      
      class Post
      {
      /**
       * @Id
       * @Column(type="integer")
       * @GeneratedValue(strategy="AUTO")
       */
      
       protected $id;
      
      /**
       *
       * @ManyToOne(targetEntity="User", inversedBy="posts")
       * @JoinColumn(nullable=false)
       */
      
      protected $author;
      
      public function setAuthor( $author )
      {
          $this->author = $author;
      }
      
      // ...skipped...
      }
      
      // Let's create user and post
      
      $user = new User();
      $em->persist($user);
      
      $post = new Post();
      $em->persist($post);
      
      $post->setUser($user);
      $user->setLastPost($post);
      
      $em->flush();
      
      // and now remove user
      
      $user = $em->find('User', 1);
      $em->remove($user);
      
      $em->flush(); 
      // "The DELETE statement conflicted with the REFERENCE 
      // constraint...".
      // Doctrine removes record from "User" table, ignoring associated records from "Post" table. If i remove "lastPost" relation 
      //from User entity everything goes OK - at first Doctrine removes assiciated posts, then removes user itself.
      

        Activity

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Alex
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: