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

Closed
opened 2026-01-22 12:50:16 +01:00 by admin · 2 comments
Owner

Originally created by @doctrinebot on GitHub (Jun 12, 2010).

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);
               }
Originally created by @doctrinebot on GitHub (Jun 12, 2010). 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); } ```
admin added the Bug label 2026-01-22 12:50:16 +01:00
admin closed this issue 2026-01-22 12:50:17 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jul 30, 2010):

Comment created by romanb:

Fixed in 69073c4b37

@doctrinebot commented on GitHub (Jul 30, 2010): Comment created by romanb: Fixed in http://github.com/doctrine/doctrine2/commit/69073c4b37ee28f988306db4965f512b70f45181
Author
Owner

@doctrinebot commented on GitHub (Jul 30, 2010):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Jul 30, 2010): Issue was closed with resolution "Fixed"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#781