Created
November 12, 2013 19:26
-
-
Save flack/7437117 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Doctrine\Tests\ORM\Functional; | |
use Doctrine\Tests\Models\CMS\CmsUser; | |
use Doctrine\ORM\Event\OnFlushEventArgs; | |
use Doctrine\ORM\Events; | |
require_once __DIR__ . '/../../TestInit.php'; | |
class UndeleteFromOnFlushTest extends \Doctrine\Tests\OrmFunctionalTestCase | |
{ | |
protected function setUp() { | |
$this->useModelSet('cms'); | |
parent::setUp(); | |
} | |
public function testUndeleteFromOnFlush() | |
{ | |
$this->_em->getEventManager()->addEventListener(Events::onFlush, new OnFlushListener); | |
$entity = new CmsUser; | |
$entity->username = 'romanb'; | |
$entity->name = 'Roman'; | |
$qb = $this->_em->createQueryBuilder(); | |
$qb->from(get_class($entity), 'c'); | |
$qb->select("count(c)"); | |
$initial = intval($qb->getQuery()->getSingleScalarResult()); | |
$this->_em->persist($entity); | |
$this->_em->flush(); | |
$this->_em->remove($entity); | |
$entity->name = 'Robin'; | |
$this->_em->flush($entity); | |
$qb = $this->_em->createQueryBuilder(); | |
$qb->from(get_class($entity), 'c'); | |
$qb->select("count(c)"); | |
$count = intval($qb->getQuery()->getSingleScalarResult()); | |
$this->assertEquals(1 + $initial, $count); | |
} | |
} | |
class OnFlushListener | |
{ | |
public function onFlush(OnFlushEventArgs $args) | |
{ | |
$em = $args->getEntityManager(); | |
$uow = $em->getUnitOfWork(); | |
$deletions = $uow->getScheduledEntityDeletions(); | |
$updates = $uow->getScheduledEntityUpdates(); | |
$undelete = array_intersect_key($deletions, $updates); | |
foreach ($undelete as $d) | |
{ | |
$em->persist($d); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment