Skip to content

Loading…

DDC-634: Merging an entity with a single valued association that has been set to null has no effect #5144

Closed
doctrinebot opened this Issue · 2 comments

1 participant

@doctrinebot

Jira issue originally created by user ccapndave:

When merging an owning entity which has NULL as one of its associated properties, merge doesn't realize that it needs to disassociate the entity.

class Doctor {

    /*** @Id @Column(type="integer") @GeneratedValue(strategy="IDENTITY") **/
    public $id;

    /****
     * @OneToMany(targetEntity="Patient", mappedBy="doctor", fetch="EAGER")
     */
    public $patients;

    public function **construct() {
        $this->patients = new ArrayCollection();
    }

}
class Patient {

    /*** @Id @Column(type="integer") @GeneratedValue(strategy="IDENTITY") **/
    public $id;

    /****
     * @OneToOne(targetEntity="Doctor", inversedBy="patients")
     * @JoinColumn(name="doctor_id", referencedColumnName="id")
     */
    public $doctor;

    public function **construct() {
    }

}

Assume that in the database there exists a doctor id=1 and a patient id=1. The patient belongs to the doctor, so the patient table has doctor_id = 1;

$p1 = new \vo\Patient();
$p1->id = 1;
$p1->doctor = null;
$em->merge($p1);
$em->flush();

This does not set doctor_id to null as expected.

It can be fixed by changing the block at line 1373 in UnitOfWork.php as follow. Apologies for not providing a patch file, I haven't quite got the hang of git yet :)

                           if ($other !== null) {
                                $targetClass = $this->_em->getClassMetadata($assoc2->targetEntityName);
                                $id = $targetClass->getIdentifierValues($other);
                                $proxy = $this->_em->getProxyFactory()->getProxy($assoc2->targetEntityName, $id);
                                $prop->setValue($managedCopy, $proxy);
                                $this->registerManaged($proxy, $id, array());
                            } else {
                                $prop->setValue($managedCopy, null);
               }
@doctrinebot

Issue was closed with resolution "Fixed"

@doctrinebot doctrinebot added this to the 2.0-BETA3 milestone
@doctrinebot doctrinebot closed this
@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.