Доступ к базе данных в слушателе в Symfony 2

Нам нужно получить доступ к информации базы данных в слушателе. Мы конфигурируем слушатель в файле service.yml. Слушатель выглядит так:

namespace company\MyBundle\Listener;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class RequestListener
{
    protected $container;

public function __construct(ContainerInterface $container)
{
    $this->container = $container;
}

public function onKernelRequest(GetResponseEvent $event)
{
...

Как мы можем получить доступ к доктрине в функции onKernelRequest?

Я пытался расширить контроллер и сделать:

        $em = $this->getDoctrine()->getEntityManager(); 

и это работает, но я думаю, что это плохая практика.

10
задан Santi 11 January 2012 в 17:12
поделиться