Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-634

Merging an entity with a single valued association that has been set to null has no effect

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.0-BETA3
    • Component/s: ORM
    • Security Level: All
    • Labels:
      None

      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);
      			   }
      

        Activity

        Show
        Roman S. Borschel added a comment - Fixed in http://github.com/doctrine/doctrine2/commit/69073c4b37ee28f988306db4965f512b70f45181

          People

          • Assignee:
            Roman S. Borschel
            Reporter:
            Dave Keen
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: