[DDC-1655][DDC-1650][DDC-1556] Fix issues with @postLoad Callback being not fired, or fired multiple times.
This commit is contained in:
parent
e6b99c2059
commit
d995c6dbdc
@ -234,20 +234,7 @@ class ObjectHydrator extends AbstractHydrator
|
||||
|
||||
$this->_hints['fetchAlias'] = $dqlAlias;
|
||||
|
||||
$entity = $this->_uow->createEntity($className, $data, $this->_hints);
|
||||
|
||||
//TODO: These should be invoked later, after hydration, because associations may not yet be loaded here.
|
||||
if (isset($this->_ce[$className]->lifecycleCallbacks[Events::postLoad])) {
|
||||
$this->_ce[$className]->invokeLifecycleCallbacks(Events::postLoad, $entity);
|
||||
}
|
||||
|
||||
$evm = $this->_em->getEventManager();
|
||||
|
||||
if ($evm->hasListeners(Events::postLoad)) {
|
||||
$evm->dispatchEvent(Events::postLoad, new LifecycleEventArgs($entity, $this->_em));
|
||||
}
|
||||
|
||||
return $entity;
|
||||
return $this->_uow->createEntity($className, $data, $this->_hints);
|
||||
}
|
||||
|
||||
private function _getEntityFromIdentityMap($className, array $data)
|
||||
|
@ -130,17 +130,6 @@ class SimpleObjectHydrator extends AbstractHydrator
|
||||
$uow = $this->_em->getUnitOfWork();
|
||||
$entity = $uow->createEntity($entityName, $data, $this->_hints);
|
||||
|
||||
//TODO: These should be invoked later, after hydration, because associations may not yet be loaded here.
|
||||
if (isset($this->class->lifecycleCallbacks[Events::postLoad])) {
|
||||
$this->class->invokeLifecycleCallbacks(Events::postLoad, $entity);
|
||||
}
|
||||
|
||||
$evm = $this->_em->getEventManager();
|
||||
|
||||
if ($evm->hasListeners(Events::postLoad)) {
|
||||
$evm->dispatchEvent(Events::postLoad, new LifecycleEventArgs($entity, $this->_em));
|
||||
}
|
||||
|
||||
$result[] = $entity;
|
||||
}
|
||||
|
||||
@ -191,4 +180,4 @@ class SimpleObjectHydrator extends AbstractHydrator
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -698,16 +698,6 @@ class BasicEntityPersister
|
||||
|
||||
$hydrator = $this->_em->newHydrator(Query::HYDRATE_OBJECT);
|
||||
$hydrator->hydrateAll($stmt, $this->_rsm, array(Query::HINT_REFRESH => true));
|
||||
|
||||
if (isset($this->_class->lifecycleCallbacks[Events::postLoad])) {
|
||||
$this->_class->invokeLifecycleCallbacks(Events::postLoad, $entity);
|
||||
}
|
||||
|
||||
$evm = $this->_em->getEventManager();
|
||||
|
||||
if ($evm->hasListeners(Events::postLoad)) {
|
||||
$evm->dispatchEvent(Events::postLoad, new LifecycleEventArgs($entity, $this->_em));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -2501,6 +2501,17 @@ class UnitOfWork implements PropertyChangedListener
|
||||
}
|
||||
}
|
||||
|
||||
if ($overrideLocalValues) {
|
||||
if (isset($class->lifecycleCallbacks[Events::postLoad])) {
|
||||
$class->invokeLifecycleCallbacks(Events::postLoad, $entity);
|
||||
}
|
||||
|
||||
|
||||
if ($this->evm->hasListeners(Events::postLoad)) {
|
||||
$this->evm->dispatchEvent(Events::postLoad, new LifecycleEventArgs($entity, $this->em));
|
||||
}
|
||||
}
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
|
144
tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1655Test.php
Normal file
144
tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1655Test.php
Normal file
@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
namespace Doctrine\Tests\ORM\Functional\Ticket;
|
||||
|
||||
/**
|
||||
* @group DDC-1655
|
||||
* @group DDC-1640
|
||||
* @group DDC-1556
|
||||
*/
|
||||
class DDC1655Test extends \Doctrine\Tests\OrmFunctionalTestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
try {
|
||||
$this->_schemaTool->createSchema(array(
|
||||
$this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC1655Foo'),
|
||||
$this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC1655Bar'),
|
||||
$this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC1655Baz'),
|
||||
));
|
||||
} catch(\Exception $e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public function testPostLoadOneToManyInheritance()
|
||||
{
|
||||
$cm = $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1655Foo');
|
||||
$this->assertEquals(array("postLoad" => array("postLoad")), $cm->lifecycleCallbacks);
|
||||
|
||||
$cm = $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1655Bar');
|
||||
$this->assertEquals(array("postLoad" => array("postLoad", "postSubLoaded")), $cm->lifecycleCallbacks);
|
||||
|
||||
$baz = new DDC1655Baz();
|
||||
$foo = new DDC1655Foo();
|
||||
$foo->baz = $baz;
|
||||
$bar = new DDC1655Bar();
|
||||
$bar->baz = $baz;
|
||||
|
||||
$this->_em->persist($foo);
|
||||
$this->_em->persist($bar);
|
||||
$this->_em->persist($baz);
|
||||
$this->_em->flush();
|
||||
$this->_em->clear();
|
||||
|
||||
$baz = $this->_em->find(get_class($baz), $baz->id);
|
||||
foreach ($baz->foos as $foo) {
|
||||
$this->assertEquals(1, $foo->loaded, "should have loaded callback counter incremented for " . get_class($foo));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that post load is not executed several times when the entity
|
||||
* is rehydrated again although its already known.
|
||||
*/
|
||||
public function testPostLoadInheritanceChild()
|
||||
{
|
||||
$bar = new DDC1655Bar();
|
||||
|
||||
$this->_em->persist($bar);
|
||||
$this->_em->flush();
|
||||
$this->_em->clear();
|
||||
|
||||
$bar = $this->_em->find(get_class($bar), $bar->id);
|
||||
$this->assertEquals(1, $bar->loaded);
|
||||
$this->assertEquals(1, $bar->subLoaded);
|
||||
|
||||
$bar = $this->_em->find(get_class($bar), $bar->id);
|
||||
$this->assertEquals(1, $bar->loaded);
|
||||
$this->assertEquals(1, $bar->subLoaded);
|
||||
|
||||
$dql = "SELECT b FROM " . __NAMESPACE__ . "\DDC1655Bar b WHERE b.id = ?1";
|
||||
$bar = $this->_em->createQuery($dql)->setParameter(1, $bar->id)->getSingleResult();
|
||||
|
||||
$this->assertEquals(1, $bar->loaded);
|
||||
$this->assertEquals(1, $bar->subLoaded);
|
||||
|
||||
$this->_em->refresh($bar);
|
||||
|
||||
$this->assertEquals(2, $bar->loaded);
|
||||
$this->assertEquals(2, $bar->subLoaded);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @Entity
|
||||
* @InheritanceType("SINGLE_TABLE")
|
||||
* @DiscriminatorMap({
|
||||
* "foo" = "DDC1655Foo",
|
||||
* "bar" = "DDC1655Bar"
|
||||
* })
|
||||
* @HasLifecycleCallbacks
|
||||
*/
|
||||
class DDC1655Foo
|
||||
{
|
||||
/** @Id @GeneratedValue @Column(type="integer") */
|
||||
public $id;
|
||||
|
||||
public $loaded = 0;
|
||||
|
||||
/**
|
||||
* @ManyToOne(targetEntity="DDC1655Baz", inversedBy="foos")
|
||||
*/
|
||||
public $baz;
|
||||
|
||||
/**
|
||||
* @PostLoad
|
||||
*/
|
||||
public function postLoad()
|
||||
{
|
||||
$this->loaded++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @Entity
|
||||
* @HasLifecycleCallbacks
|
||||
*/
|
||||
class DDC1655Bar extends DDC1655Foo
|
||||
{
|
||||
public $subLoaded;
|
||||
|
||||
/**
|
||||
* @PostLoad
|
||||
*/
|
||||
public function postSubLoaded()
|
||||
{
|
||||
$this->subLoaded++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @Entity
|
||||
*/
|
||||
class DDC1655Baz
|
||||
{
|
||||
/** @Id @GeneratedValue @Column(type="integer") */
|
||||
public $id;
|
||||
|
||||
/**
|
||||
* @OneToMany(targetEntity="DDC1655Foo", mappedBy="baz")
|
||||
*/
|
||||
public $foos = array();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user