Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-2822

Replacing object in a OneToOne with OrphanRemoval=true isn't working as expected

    Details

    • Type: Bug Bug
    • Status: In Progress
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: 2.4, 2.4.1
    • Fix Version/s: None
    • Component/s: ORM
    • Security Level: All
    • Labels:
    • Environment:
      PHP 5.4

      Description

      So I have a class defined like this:

      class PhoneSettings {
          //[...]
      
          /**
           * @OneToOne(targetEntity="Medium", cascade={"persist", "remove"}, orphanRemoval=true)
           * @JoinColumn(name="medium_id", referencedColumnName="medium_id", nullable=true, onDelete="SET NULL")
           **/
          protected $medium = null;
      
          //[...]    
      }
      

      And class Medium has no reference to the class Settings.

      Now suppose I have a $Settings object that is already persisted and has been correctly loaded. Also suppose that the $Settings object has a $medium (that is, $Settings->medium = $OldMedium)

      Now suppose I do:

      $Settings->medium = $NewMedium;

      Where $NewMedium is a different Medium object.

      When I persist $Settings, Doctrine does delete $OldMedium from the DB, but the problem is that it also deletes $NewMedium ...

      I have tried removing onDelete="SET NULL", but then I receive a "cannot delete, constraint failed" error...

        Activity

        Hide
        Benjamin Eberlei added a comment -

        Cannot reproduce this, for me it works, see the SQL log:

        CREATE TABLE DDC2822Settings (id INTEGER NOT NULL, medium_id INTEGER DEFAULT NULL, PRIMARY KEY(id), CONSTRAINT FK_B06D3D1FE252B6A5 FOREIGN KEY (medium_id) REFERENCES DDC2822Medium (id) ON DELETE SET NULL NOT DEFERRABLE INITIALLY IMMEDIATE)
        CREATE UNIQUE INDEX UNIQ_B06D3D1FE252B6A5 ON DDC2822Settings (medium_id)
        CREATE TABLE DDC2822Medium (id INTEGER NOT NULL, PRIMARY KEY(id))
        "START TRANSACTION"
        INSERT INTO DDC2822Medium (id) VALUES (null)
        INSERT INTO DDC2822Settings (medium_id) VALUES (?) with {"1":1}
        "COMMIT"
        "START TRANSACTION"
        INSERT INTO DDC2822Medium (id) VALUES (null)
        UPDATE DDC2822Settings SET medium_id = ? WHERE id = ? with [2,1]
        DELETE FROM DDC2822Medium WHERE id = ? with [1]
        "COMMIT"
        

        Testcase attached.

        Can you show the code that creates $newMedium in your code?

        Show
        Benjamin Eberlei added a comment - Cannot reproduce this, for me it works, see the SQL log: CREATE TABLE DDC2822Settings (id INTEGER NOT NULL, medium_id INTEGER DEFAULT NULL, PRIMARY KEY(id), CONSTRAINT FK_B06D3D1FE252B6A5 FOREIGN KEY (medium_id) REFERENCES DDC2822Medium (id) ON DELETE SET NULL NOT DEFERRABLE INITIALLY IMMEDIATE) CREATE UNIQUE INDEX UNIQ_B06D3D1FE252B6A5 ON DDC2822Settings (medium_id) CREATE TABLE DDC2822Medium (id INTEGER NOT NULL, PRIMARY KEY(id)) "START TRANSACTION" INSERT INTO DDC2822Medium (id) VALUES ( null ) INSERT INTO DDC2822Settings (medium_id) VALUES (?) with { "1" :1} "COMMIT" "START TRANSACTION" INSERT INTO DDC2822Medium (id) VALUES ( null ) UPDATE DDC2822Settings SET medium_id = ? WHERE id = ? with [2,1] DELETE FROM DDC2822Medium WHERE id = ? with [1] "COMMIT" Testcase attached. Can you show the code that creates $newMedium in your code?
        Hide
        Felipe Guaycuru added a comment -

        Just tested on newest version and I can confirm it no longer happens, so it seems to be fixed!

        Show
        Felipe Guaycuru added a comment - Just tested on newest version and I can confirm it no longer happens, so it seems to be fixed!

          People

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

            Dates

            • Created:
              Updated: