2009-07-18 22:06:30 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Functional;
|
2010-03-15 01:16:15 +03:00
|
|
|
use Doctrine\ORM\Event\PreUpdateEventArgs;
|
2009-07-18 22:06:30 +04:00
|
|
|
|
|
|
|
require_once __DIR__ . '/../../TestInit.php';
|
|
|
|
|
|
|
|
class LifecycleCallbackTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|
|
|
{
|
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
try {
|
|
|
|
$this->_schemaTool->createSchema(array(
|
2009-11-13 19:39:28 +03:00
|
|
|
$this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity'),
|
2009-12-11 03:20:37 +03:00
|
|
|
$this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\LifecycleCallbackTestUser'),
|
|
|
|
$this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\LifecycleCallbackCascader'),
|
2009-07-18 22:06:30 +04:00
|
|
|
));
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
// Swallow all exceptions. We do not test the schema tool here.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPreSavePostSaveCallbacksAreInvoked()
|
|
|
|
{
|
|
|
|
$entity = new LifecycleCallbackTestEntity;
|
|
|
|
$entity->value = 'hello';
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($entity);
|
2009-07-18 22:06:30 +04:00
|
|
|
$this->_em->flush();
|
|
|
|
|
2009-07-24 15:33:38 +04:00
|
|
|
$this->assertTrue($entity->prePersistCallbackInvoked);
|
|
|
|
$this->assertTrue($entity->postPersistCallbackInvoked);
|
2009-07-18 22:06:30 +04:00
|
|
|
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery("select e from Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity e");
|
2009-08-03 21:18:37 +04:00
|
|
|
$result = $query->getResult();
|
2009-07-18 22:06:30 +04:00
|
|
|
$this->assertTrue($result[0]->postLoadCallbackInvoked);
|
|
|
|
|
|
|
|
$result[0]->value = 'hello again';
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->assertEquals('changed from preUpdate callback!', $result[0]->value);
|
|
|
|
}
|
2009-11-13 19:39:28 +03:00
|
|
|
|
2011-10-23 19:39:53 +04:00
|
|
|
public function testPreFlushCallbacksAreInvoked()
|
|
|
|
{
|
|
|
|
$entity = new LifecycleCallbackTestEntity;
|
|
|
|
$entity->value = 'hello';
|
|
|
|
$this->_em->persist($entity);
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->assertTrue($entity->prePersistCallbackInvoked);
|
|
|
|
$this->assertTrue($entity->preFlushCallbackInvoked);
|
|
|
|
|
|
|
|
$entity->preFlushCallbackInvoked = false;
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->assertTrue($entity->preFlushCallbackInvoked);
|
|
|
|
|
|
|
|
$entity->value = 'bye';
|
|
|
|
$entity->preFlushCallbackInvoked = false;
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->assertTrue($entity->preFlushCallbackInvoked);
|
|
|
|
}
|
|
|
|
|
2009-11-13 19:39:28 +03:00
|
|
|
public function testChangesDontGetLost()
|
|
|
|
{
|
|
|
|
$user = new LifecycleCallbackTestUser;
|
|
|
|
$user->setName('Bob');
|
2009-12-09 15:37:57 +03:00
|
|
|
$user->setValue('value');
|
2009-11-13 19:39:28 +03:00
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$user->setName('Alice');
|
|
|
|
$this->_em->flush(); // Triggers preUpdate
|
|
|
|
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$user2 = $this->_em->find(get_class($user), $user->getId());
|
|
|
|
|
|
|
|
$this->assertEquals('Alice', $user2->getName());
|
|
|
|
$this->assertEquals('Hello World', $user2->getValue());
|
|
|
|
}
|
2009-12-08 01:10:40 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-194
|
|
|
|
*/
|
|
|
|
public function testGetReferenceWithPostLoadEventIsDelayedUntilProxyTrigger()
|
|
|
|
{
|
|
|
|
$entity = new LifecycleCallbackTestEntity;
|
|
|
|
$entity->value = 'hello';
|
|
|
|
$this->_em->persist($entity);
|
|
|
|
$this->_em->flush();
|
|
|
|
$id = $entity->getId();
|
|
|
|
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$reference = $this->_em->getReference('Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity', $id);
|
|
|
|
$this->assertFalse($reference->postLoadCallbackInvoked);
|
|
|
|
|
2011-05-20 22:50:03 +04:00
|
|
|
$reference->getValue(); // trigger proxy load
|
2009-12-08 01:10:40 +03:00
|
|
|
$this->assertTrue($reference->postLoadCallbackInvoked);
|
|
|
|
}
|
2009-12-11 03:20:37 +03:00
|
|
|
|
2011-01-23 19:26:11 +03:00
|
|
|
/**
|
|
|
|
* @group DDC-958
|
|
|
|
*/
|
|
|
|
public function testPostLoadTriggeredOnRefresh()
|
|
|
|
{
|
|
|
|
$entity = new LifecycleCallbackTestEntity;
|
|
|
|
$entity->value = 'hello';
|
|
|
|
$this->_em->persist($entity);
|
|
|
|
$this->_em->flush();
|
|
|
|
$id = $entity->getId();
|
|
|
|
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$reference = $this->_em->find('Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity', $id);
|
|
|
|
$this->assertTrue($reference->postLoadCallbackInvoked);
|
|
|
|
$reference->postLoadCallbackInvoked = false;
|
|
|
|
|
|
|
|
$this->_em->refresh($reference);
|
|
|
|
$this->assertTrue($reference->postLoadCallbackInvoked, "postLoad should be invoked when refresh() is called.");
|
|
|
|
}
|
|
|
|
|
2009-12-11 03:20:37 +03:00
|
|
|
/**
|
|
|
|
* @group DDC-113
|
|
|
|
*/
|
|
|
|
public function testCascadedEntitiesCallsPrePersist()
|
|
|
|
{
|
2010-04-01 00:47:35 +04:00
|
|
|
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
|
2009-12-18 15:30:19 +03:00
|
|
|
|
2009-12-11 03:20:37 +03:00
|
|
|
$e1 = new LifecycleCallbackTestEntity;
|
|
|
|
$e2 = new LifecycleCallbackTestEntity;
|
|
|
|
|
|
|
|
$c = new LifecycleCallbackCascader();
|
2009-12-18 15:30:19 +03:00
|
|
|
$this->_em->persist($c);
|
|
|
|
|
2009-12-11 03:20:37 +03:00
|
|
|
$c->entities[] = $e1;
|
|
|
|
$c->entities[] = $e2;
|
2009-12-18 15:30:19 +03:00
|
|
|
$e1->cascader = $c;
|
|
|
|
$e2->cascader = $c;
|
|
|
|
|
|
|
|
//$this->_em->persist($c);
|
2009-12-11 03:20:37 +03:00
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->assertTrue($e1->prePersistCallbackInvoked);
|
|
|
|
$this->assertTrue($e2->prePersistCallbackInvoked);
|
|
|
|
}
|
2010-02-11 01:17:43 +03:00
|
|
|
|
|
|
|
public function testLifecycleCallbacksGetInherited()
|
|
|
|
{
|
|
|
|
$childMeta = $this->_em->getClassMetadata(__NAMESPACE__ . '\LifecycleCallbackChildEntity');
|
|
|
|
$this->assertEquals(array('prePersist' => array(0 => 'doStuff')), $childMeta->lifecycleCallbacks);
|
|
|
|
}
|
2010-03-15 01:16:15 +03:00
|
|
|
|
|
|
|
public function testLifecycleListener_ChangeUpdateChangeSet()
|
|
|
|
{
|
|
|
|
$listener = new LifecycleListenerPreUpdate;
|
|
|
|
$this->_em->getEventManager()->addEventListener(array('preUpdate'), $listener);
|
|
|
|
|
|
|
|
$user = new LifecycleCallbackTestUser;
|
|
|
|
$user->setName('Bob');
|
|
|
|
$user->setValue('value');
|
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$dql = "SELECT u FROM Doctrine\Tests\ORM\Functional\LifecycleCallbackTestUser u WHERE u.name = 'Bob'";
|
|
|
|
$bob = $this->_em->createQuery($dql)->getSingleResult();
|
|
|
|
$bob->setName('Alice');
|
|
|
|
|
|
|
|
$this->_em->flush(); // preUpdate reverts Alice to Bob
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$this->_em->getEventManager()->removeEventListener(array('preUpdate'), $listener);
|
|
|
|
|
|
|
|
$bob = $this->_em->createQuery($dql)->getSingleResult();
|
|
|
|
|
|
|
|
$this->assertEquals('Bob', $bob->getName());
|
|
|
|
}
|
2009-11-13 19:39:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @Entity @HasLifecycleCallbacks */
|
|
|
|
class LifecycleCallbackTestUser {
|
2010-02-11 01:17:43 +03:00
|
|
|
/** @Id @Column(type="integer") @GeneratedValue */
|
2009-11-13 19:39:28 +03:00
|
|
|
private $id;
|
|
|
|
/** @Column(type="string") */
|
|
|
|
private $value;
|
|
|
|
/** @Column(type="string") */
|
|
|
|
private $name;
|
|
|
|
public function getId() {return $this->id;}
|
|
|
|
public function getValue() {return $this->value;}
|
|
|
|
public function setValue($value) {$this->value = $value;}
|
|
|
|
public function getName() {return $this->name;}
|
|
|
|
public function setName($name) {$this->name = $name;}
|
|
|
|
/** @PreUpdate */
|
|
|
|
public function testCallback() {$this->value = 'Hello World';}
|
2009-07-18 22:06:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Entity
|
2009-08-26 21:14:03 +04:00
|
|
|
* @HasLifecycleCallbacks
|
2009-08-28 21:25:28 +04:00
|
|
|
* @Table(name="lc_cb_test_entity")
|
2009-07-18 22:06:30 +04:00
|
|
|
*/
|
|
|
|
class LifecycleCallbackTestEntity
|
|
|
|
{
|
|
|
|
/* test stuff */
|
2009-07-24 15:33:38 +04:00
|
|
|
public $prePersistCallbackInvoked = false;
|
|
|
|
public $postPersistCallbackInvoked = false;
|
2009-07-18 22:06:30 +04:00
|
|
|
public $postLoadCallbackInvoked = false;
|
|
|
|
|
2011-10-23 19:39:53 +04:00
|
|
|
public $preFlushCallbackInvoked = false;
|
|
|
|
|
2009-07-18 22:06:30 +04:00
|
|
|
/**
|
|
|
|
* @Id @Column(type="integer")
|
|
|
|
* @GeneratedValue(strategy="AUTO")
|
|
|
|
*/
|
|
|
|
private $id;
|
|
|
|
/**
|
2009-12-11 03:20:37 +03:00
|
|
|
* @Column(type="string", nullable=true)
|
2009-07-18 22:06:30 +04:00
|
|
|
*/
|
|
|
|
public $value;
|
2009-12-08 01:10:40 +03:00
|
|
|
|
2009-12-11 03:20:37 +03:00
|
|
|
/**
|
|
|
|
* @ManyToOne(targetEntity="LifecycleCallbackCascader")
|
|
|
|
* @JoinColumn(name="cascader_id", referencedColumnName="id")
|
|
|
|
*/
|
|
|
|
public $cascader;
|
|
|
|
|
2009-12-08 01:10:40 +03:00
|
|
|
public function getId() {
|
|
|
|
return $this->id;
|
|
|
|
}
|
2009-07-18 22:06:30 +04:00
|
|
|
|
2011-05-20 22:50:03 +04:00
|
|
|
public function getValue() {
|
|
|
|
return $this->value;
|
|
|
|
}
|
|
|
|
|
2009-07-24 15:33:38 +04:00
|
|
|
/** @PrePersist */
|
|
|
|
public function doStuffOnPrePersist() {
|
|
|
|
$this->prePersistCallbackInvoked = true;
|
2009-07-18 22:06:30 +04:00
|
|
|
}
|
|
|
|
|
2009-07-24 15:33:38 +04:00
|
|
|
/** @PostPersist */
|
|
|
|
public function doStuffOnPostPersist() {
|
|
|
|
$this->postPersistCallbackInvoked = true;
|
2009-07-18 22:06:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @PostLoad */
|
|
|
|
public function doStuffOnPostLoad() {
|
|
|
|
$this->postLoadCallbackInvoked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @PreUpdate */
|
|
|
|
public function doStuffOnPreUpdate() {
|
|
|
|
$this->value = 'changed from preUpdate callback!';
|
|
|
|
}
|
2011-10-23 19:39:53 +04:00
|
|
|
|
|
|
|
/** @PreFlush */
|
|
|
|
public function doStuffOnPreFlush() {
|
|
|
|
$this->preFlushCallbackInvoked = true;
|
|
|
|
}
|
2009-12-11 03:20:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Entity
|
|
|
|
* @Table(name="lc_cb_test_cascade")
|
|
|
|
*/
|
|
|
|
class LifecycleCallbackCascader
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @Id @Column(type="integer")
|
|
|
|
* @GeneratedValue(strategy="AUTO")
|
|
|
|
*/
|
|
|
|
private $id;
|
|
|
|
|
|
|
|
/**
|
2009-12-18 15:30:19 +03:00
|
|
|
* @OneToMany(targetEntity="LifecycleCallbackTestEntity", mappedBy="cascader", cascade={"persist"})
|
2009-12-11 03:20:37 +03:00
|
|
|
*/
|
|
|
|
public $entities;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->entities = new \Doctrine\Common\Collections\ArrayCollection();
|
|
|
|
}
|
2010-02-11 01:17:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @MappedSuperclass @HasLifecycleCallbacks */
|
|
|
|
class LifecycleCallbackParentEntity {
|
|
|
|
/** @PrePersist */
|
|
|
|
function doStuff() {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @Entity @Table(name="lc_cb_childentity") */
|
|
|
|
class LifecycleCallbackChildEntity extends LifecycleCallbackParentEntity {
|
|
|
|
/** @Id @Column(type="integer") @GeneratedValue */
|
|
|
|
private $id;
|
|
|
|
}
|
2010-03-15 01:16:15 +03:00
|
|
|
|
|
|
|
class LifecycleListenerPreUpdate
|
|
|
|
{
|
|
|
|
public function preUpdate(PreUpdateEventArgs $eventArgs)
|
|
|
|
{
|
|
|
|
$eventArgs->setNewValue('name', 'Bob');
|
|
|
|
}
|
2011-10-15 19:31:09 +04:00
|
|
|
}
|