Created
December 7, 2011 22:26
-
-
Save j/1445015 to your computer and use it in GitHub Desktop.
How to pass Request to a DoctrineListener
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 AcmeDemo\MyBundle\Listener; | |
use Doctrine\ORM\Event\OnFlushEventArgs; | |
use Symfony\Component\HttpFoundation\Request; | |
class MyListener | |
{ | |
/** | |
* @var \Symfony\Component\HttpFoundation\Session | |
*/ | |
private $session; | |
public function __construct(Request $request) | |
{ | |
$this->session = $request->getSession(); | |
} | |
public function onFlush(OnFlushEventArgs $eventArgs) | |
{ | |
/** @var $entityManager \Doctrine\ORM\EntityManager */ | |
$entityManager = $eventArgs->getEntityManager(); | |
/** @var $unitOfWork \Doctrine\ORM\UnitOfWork */ | |
$unitOfWork = $entityManager->getUnitofWork(); | |
// do stuff to the new entities | |
foreach ($unitOfWork->getScheduledEntityInsertions() as $entity) { | |
if ($entity instanceof Visitor) { | |
$variation = $entityManager->getReference('AcmeDemoMyBundle:Variation', $this->session->get('variation')->getId()); | |
$variation->incrementVisitorCount(); | |
// ... add variation to unit of work to be persisted / flushed | |
} | |
} | |
} | |
} |
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
services: | |
mylistener.listener: | |
class: AcmeDemo\MyBundle\Listener\MyListener | |
scope: request | |
arguments: [@request] | |
tags: | |
- { name: doctrine.event_listener, event: onFlush, method: onFlush } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment