I would like to know what is the best practice for EventDispatcher injection in EntityRepository class.
First, using global is a very bad practice. I strongly advise you not to do this.
Second, Injecting services into a repository doesn't seem like a good idea. It will often break laws like the Single Responsibility Principle.
I'd create a manager that will wrap the methods of your repository, and will trigger the events you need. See how to inject repository to a service for further information.
services.yml
services:
    my_manager:
        class: Acme\FooBundle\MyManager
        arguments:
            - @acme_foo.repository
            - @event_dispatcher
    acme_foo.repository:
        class: Acme\FooBundle\Repository\FooRepository
        factory_service: doctrine.orm.entity_manager
        factory_method: getRepository
        arguments:
            - "AcmeFooBundle:Foo"
Acme\FooBundle\MyManager
use Acme\FooBundle\Repository\FooRepository;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class MyManager
{
    protected $repository;
    protected $dispatcher;
    public function __construct(FooRepository $repository, EventDispatcherInterface $dispatcher)
    {
        $this->repository = $repository;
        $this->dispatcher = $dispatcher;
    }
    public function findFooEntities(array $options = array())
    {
        $event = new PreFindEvent;
        $event->setOptions($options);
        $this->dispatcher->dispatch('find_foo.pre_find', $event);
        $results = $this->repository->findFooEntities($event->getOptions());
        $event = new PostFindEvent;
        $event->setResults($results);
        $this->dispatcher->dispatch('find_foo.post_find', $event);
        return $event->getResults();
    }
}
Then you can use it in your controller, just like a service.
$this->get('my_manager')->findFooEntities($options);
However, if you really need to inject the event dispatcher into your entity, you can do this
services.yml
services:
    acme_foo.repository:
        class: Acme\FooBundle\Repository\FooRepository
        factory_service: doctrine.orm.entity_manager
        factory_method: getRepository
        arguments:
            - "AcmeFooBundle:Foo"
        calls:
            - [ "setEventDispatcher", [ @event_dispatcher ] ]
Then you just have to add the setEventDispatcher method to your repository.
Acme\FooBundle\Repository\FooRepository
class FooRepository extends EntityRepository
{
    protected $dispatcher;
    public function setEventDispatcher(EventDispatcherInterface $dispatcher)
    {
        $this->dispatcher = $dispatcher;
    }
    public function findFooEntities(array $options = array())
    {
        $dispatcher = $this->dispatcher;
        // ...
    }
}
Just make sure you call the service and not the repository when using it in the controller.
DO
$this->get('acme_foo.repository')->findFooEntities();
DON'T
$this->getDoctrine()->getManager()->getRepository('AcmeFooBundle:Foo')->findFooEntities();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With