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

        Alex created issue -
        Alex made changes -
        Field Original Value New Value
        Description class Blog
        {

        }
        /**
         * @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.

        Alex made changes -
        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.

        {code}
        /**
         * @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.
        {code}
        Alex made changes -
        Description {code}
        /**
         * @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.
        {code}
        {code}
        /**
         * @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.
        {code}
        Benjamin Eberlei made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Resolution Invalid [ 6 ]

          People

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

            Dates

            • Created:
              Updated:
              Resolved: