Details
Description
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); }
Fixed in http://github.com/doctrine/doctrine2/commit/69073c4b37ee28f988306db4965f512b70f45181