Skip to content

Instantly share code, notes, and snippets.

@j
Created December 7, 2011 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j/1445015 to your computer and use it in GitHub Desktop.
Save j/1445015 to your computer and use it in GitHub Desktop.
How to pass Request to a DoctrineListener
<?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
}
}
}
}
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