2009-07-18 22:06:30 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Functional;
|
|
|
|
|
|
|
|
require_once __DIR__ . '/../../TestInit.php';
|
|
|
|
|
|
|
|
class LifecycleCallbackTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|
|
|
{
|
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
try {
|
|
|
|
$this->_schemaTool->createSchema(array(
|
|
|
|
$this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity')
|
|
|
|
));
|
|
|
|
} 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");
|
|
|
|
$result = $query->getResultList();
|
|
|
|
$this->assertTrue($result[0]->postLoadCallbackInvoked);
|
|
|
|
|
|
|
|
$result[0]->value = 'hello again';
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->assertEquals('changed from preUpdate callback!', $result[0]->value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Entity
|
|
|
|
* @LifecycleListener
|
|
|
|
* @Table(name="lifecycle_callback_test_entity")
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Id @Column(type="integer")
|
|
|
|
* @GeneratedValue(strategy="AUTO")
|
|
|
|
*/
|
|
|
|
private $id;
|
|
|
|
/**
|
|
|
|
* @Column(type="string", length=255)
|
|
|
|
*/
|
|
|
|
public $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!';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|