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

          People

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

            Dates

            • Created:
              Updated:
              Resolved: