Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-1974

@OneToOne and @InheritanceType cause Duplicate Key Errors when changing the entity

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Won't Fix
    • Affects Version/s: 2.2.2
    • Fix Version/s: None
    • Component/s: ORM
    • Security Level: All
    • Labels:
    • Environment:
      Win 7, PHP 5.3, MySQL InnoDB

      Description

      <?php
      
      /** @Entity */
      class A
      {
          /**
           * @Id @Column(name="id", type="integer")
           */
          private $id;
      
          /**
           * @OneToOne(targetEntity="B", mappedBy="a")
           */
          private $b;
      }
      
      /** 
       * @Entity 
       * @InheritanceType("SINGLE_TABLE")
       */
      class B
      {
          /**
           * @Id @Column(name="id", type="integer", nullable=false)
           */
          private $id;
      
          /**
           * @OneToOne(targetEntity="A", inversedBy="b")
           */
          private $a;
      }
      
      /** 
       * @Entity 
       */
      class B_sub extends B
      {
          /** @Column(type="string") */
          private $sub;
      }
      
      /** 
       * @Entity 
       */
      class B_sub2 extends B
      {
          /** @Column(type="string") */
          private $sub2;
      }
      

      The code creates several tables and a UNIQUE KEY constraint on B (a_id)

      Now trying to accomplish the following sequence of action:

      $B = new B_sub();
      $A = new A();
      
      $A->set('b', $B);
      $B->set('a', $A);
      
      $em->persist($A);
      $em->persist($B);
      //works fine and creates the correct DB entries
      $em->flush();
      
      $B_new = new B_sub2();
      $B_old = $A->get('b');
      $A->set('b', $B_new);
      $B_new->set('a', $A);
      
      $em->remove($B_old);
      $em->persist($A);
      $em->persist($B_new);
      //does not work, because B_new is inserted first and fails the UNIQUE KEY constraint (both B_new and B_old refer to A)
      $em->flush();
      

      The result is a "Duplicate entry '1' for key 'UNIQ_7F6BFCEBE26CCE03'"

        Activity

        Hide
        Philipp Dobrigkeit added a comment -

        Current workaround is to change the ownership of the relationship

        <?php
        
        /** @Entity */
        class A
        {
            /**
             * @Id @Column(name="id", type="integer")
             */
            private $id;
        
            /**
             * @OneToOne(targetEntity="B", inversedBy="a")
             */
            private $b;
        }
        
        /** 
         * @Entity 
         * @InheritanceType("SINGLE_TABLE")
         */
        class B
        {
            /**
             * @Id @Column(name="id", type="integer", nullable=false)
             */
            private $id;
        
            /**
             * @OneToOne(targetEntity="A", mappedBy="b")
             */
            private $a;
        }
        
        Show
        Philipp Dobrigkeit added a comment - Current workaround is to change the ownership of the relationship <?php /** @Entity */ class A { /** * @Id @Column(name= "id" , type= "integer" ) */ private $id; /** * @OneToOne(targetEntity= "B" , inversedBy= "a" ) */ private $b; } /** * @Entity * @InheritanceType( "SINGLE_TABLE" ) */ class B { /** * @Id @Column(name= "id" , type= "integer" , nullable=false) */ private $id; /** * @OneToOne(targetEntity= "A" , mappedBy= "b" ) */ private $a; }
        Hide
        Benjamin Eberlei added a comment -

        Insert before Removal is by design, see discussion here http://www.doctrine-project.org/jira/browse/DDC-601

        Show
        Benjamin Eberlei added a comment - Insert before Removal is by design, see discussion here http://www.doctrine-project.org/jira/browse/DDC-601

          People

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

            Dates

            • Created:
              Updated:
              Resolved: