Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-1666

orphanRemoval does not work with oneToOne: Duplicate entry Error

    Details

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

      Description

      orphanRemoval does not work with oneToOne.
      Im getting "duplicate entry" errors after replacing the oneToOne object.
      Doctrine seems to insert the new data before removing the old one.
      if i remove the unique constraint by hand in the database it works.

      Mysql Error
      /*
      Error:
      SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'UNIQ_9D8DDB05579B502F'
      */
      
      Models and YAML Mapping
      /*
      Contact:
        type: entity
        table: contact
        fields:
          _id:
            id: true
            type: integer
            unsigned: true
            nullable: false
            generator:
              strategy: AUTO
            column: id
        oneToOne:
          _standingData:
            targetEntity: StandingData
            mappedBy: _contact
            cascade: ["persist", "merge", "remove"] 
            orphanRemoval: true
      */
      
      class Contact
      {
          private $_id;
          private $_standingData;
      
          public function newStandingData(StandingData $sd)
          {
              $this->_standingData = $sd;
              $sd->setContact($this);
          }
      }
      
      /*
      StandingData:
        type: entity
        table: standing_data
        fields:
          _id:
            id: true
            type: integer
            unsigned: true
            nullable: false
            generator:
              strategy: AUTO
            column: id
        oneToOne:
          _contact:
            targetEntity: Contact
            inversedBy: _standingData
            joinColumns:
              contact_id:
                referencedColumnName: id
                onDelete: cascade
      */
      
      class StandingData
      {
          private $_id;
          private $_contact;
      
          public function setContact(Contact $c)
          {
              $this->_contact = $c;
          }
      }
      
      Script
      // Create new Contact
      $contact = new Contact();
      $contact->newStandingData(new \StandingData());
      $em->persist($contact);
      $em->flush();
      
      // Try to change StandingData
      $contact->newStandingData(new \StandingData());
      $em->flush();
      
      
      

        Activity

        Mario Knippfeld created issue -
        Mario Knippfeld made changes -
        Field Original Value New Value
        Description orphanRemoval does not work with oneToOne.
        Im getting "duplicate entry" errors after replacing the oneToOne object:
        {code:title=Mysql Error}
        /*
        Error:
        SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'UNIQ_9D8DDB05579B502F'
        */
        {code}

        {code:title=Models and YAML Mapping}

        /*
        Contact:
          type: entity
          table: contact
          fields:
            _id:
              id: true
              type: integer
              unsigned: true
              nullable: false
              generator:
                strategy: AUTO
              column: id
          oneToOne:
            _standingData:
              targetEntity: StandingData
              mappedBy: _contact
              cascade: ["persist", "merge", "remove"]
              orphanRemoval: true
        */

        class Contact
        {
            private $_id;
            private $_standingData;

            public function newStandingData(StandingData $sd)
            {
                $this->_standingData = $sd;
                $sd->setContact($this);
            }
        }

        /*
        StandingData:
          type: entity
          table: standing_data
          fields:
            _id:
              id: true
              type: integer
              unsigned: true
              nullable: false
              generator:
                strategy: AUTO
              column: id
          oneToOne:
            _contact:
              targetEntity: Contact
              inversedBy: _standingData
              joinColumns:
                contact_id:
                  referencedColumnName: id
                  onDelete: cascade
        */

        class StandingData
        {
            private $_id;
            private $_contact;

            public function setContact(Contact $c)
            {
                $this->_contact = $c;
            }
        }
        {code}


        {code:title=Script}
        // Create new Contact
        $contact = new Contact();
        $contact->newStandingData(new \StandingData());
        $em->persist($contact);
        $em->flush();

        // Try to change StandingData
        $contact->newStandingData(new \StandingData());
        $em->flush();


        {code}
        orphanRemoval does not work with oneToOne.
        Im getting "duplicate entry" errors after replacing the oneToOne object.
        Doctrine seems to insert the new data before removing the old one.
        if i remove the unique constraint by hand in the database it works.

        {code:title=Mysql Error}
        /*
        Error:
        SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'UNIQ_9D8DDB05579B502F'
        */
        {code}

        {code:title=Models and YAML Mapping}

        /*
        Contact:
          type: entity
          table: contact
          fields:
            _id:
              id: true
              type: integer
              unsigned: true
              nullable: false
              generator:
                strategy: AUTO
              column: id
          oneToOne:
            _standingData:
              targetEntity: StandingData
              mappedBy: _contact
              cascade: ["persist", "merge", "remove"]
              orphanRemoval: true
        */

        class Contact
        {
            private $_id;
            private $_standingData;

            public function newStandingData(StandingData $sd)
            {
                $this->_standingData = $sd;
                $sd->setContact($this);
            }
        }

        /*
        StandingData:
          type: entity
          table: standing_data
          fields:
            _id:
              id: true
              type: integer
              unsigned: true
              nullable: false
              generator:
                strategy: AUTO
              column: id
          oneToOne:
            _contact:
              targetEntity: Contact
              inversedBy: _standingData
              joinColumns:
                contact_id:
                  referencedColumnName: id
                  onDelete: cascade
        */

        class StandingData
        {
            private $_id;
            private $_contact;

            public function setContact(Contact $c)
            {
                $this->_contact = $c;
            }
        }
        {code}


        {code:title=Script}
        // Create new Contact
        $contact = new Contact();
        $contact->newStandingData(new \StandingData());
        $em->persist($contact);
        $em->flush();

        // Try to change StandingData
        $contact->newStandingData(new \StandingData());
        $em->flush();


        {code}
        Benjamin Eberlei made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Resolution Can't Fix [ 7 ]
        Mario Knippfeld made changes -
        Resolution Can't Fix [ 7 ]
        Status Resolved [ 5 ] Reopened [ 4 ]
        Benjamin Eberlei made changes -
        Workflow jira [ 13471 ] jira-feedback [ 14105 ]
        Benjamin Eberlei made changes -
        Workflow jira-feedback [ 14105 ] jira-feedback2 [ 15969 ]
        Benjamin Eberlei made changes -
        Workflow jira-feedback2 [ 15969 ] jira-feedback3 [ 18223 ]
        Benjamin Eberlei made changes -
        Status Reopened [ 4 ] Resolved [ 5 ]
        Fix Version/s 2.3.3 [ 10329 ]
        Resolution Fixed [ 1 ]

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Mario Knippfeld
          • Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: