简体   繁体   中英

Doctrine 2, removing association SET NULL returns empty object instead of NULL

With Doctrine, I have 2 classes The relation they have is OneToMany/ManyToOne as such:

>> ZipGroup Class
/**
 * @ORM\OneToMany(targetEntity="License", mappedBy="zipGroup")
 */
protected $licenses;

>> License Class
/**
 * @ORM\ManyToOne(targetEntity="ZipGroup", inversedBy="licenses")
 * @ORM\JoinColumn(name="zipGroup_id", referencedColumnName="id", onDelete="SET NULL")
 */
protected $zipGroup;

During my unit tests, I verify when I remove one zipgroup, that it's correctly set to null in the license. The problem is, when I load the License from the repository, it seems that I get a dummy empty object (no ID) instead of NULL. Maybe it's a cached version?

...
$this->_em->remove($zipGroup);
$this->_em->flush();

$zipGroup = $this->_em->getRepository("BarcodePhpBundle:ZipGroup")->findOneByName("ZipGroupName");
$this->assertEquals(NULL, $zipGroup);

// License is NOT removed
$license = $this->_em->getRepository("BarcodePhpBundle:License")->findOneByPrice(25);
$this->assertEquals(25, $license->getPrice());

// BUG HERE, that value is not null, but I get an dummy object
$this->assertEquals(NULL, $license->getZipGroup());

Looking into the database, the row is NULL, but Doctrine doesn't say NULL...

Any idea?

It looks like this problem: How to handle related null-objects correctly in symfony 1.4

The entity manager was not cleared after the removal. In order to clear the identity map, the following code has to be called:

$this->_em->clear();

From: https://groups.google.com/forum/#!msg/doctrine-user/73c6h4JgLRY/sQvBjSjzrWsJ

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM