Skip to content

Loading…

DDC-2822: Replacing object in a OneToOne with OrphanRemoval=true isn't working as expected #3575

Open
doctrinebot opened this Issue · 2 comments

2 participants

@doctrinebot

Jira issue originally created by user guaycuru:

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...

@doctrinebot

Comment created by @beberlei:

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?

@doctrinebot

Comment created by guaycuru:

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

@beberlei beberlei was assigned by doctrinebot
@doctrinebot doctrinebot added the Bug label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.