Injecting dependency into entity repository Injecting dependency into entity repository symfony symfony

Injecting dependency into entity repository


Problem is that repository classes are not part of the Symfony2 codebase as they are part of Doctrine2, so they do not take advantage of the DIC; this is why you can't go for injection in one place for all repositories.

I would advice you to use a different approach. For example you can create a service layer on top of the repositories and actually inject the class you want through a factory in that layer.

Otherwise you could also define repositories as services this way:

<service id="your_namespace.repository.repos_name"          class="%your_namespace.repository.repos_name%"          factory-service="doctrine" factory-method="getRepository">  <argument>entity_name</argument>  <argument>entity_manager_name</argument>  <call method="yourSetter">      <argument>your_argument</argument>  </call></service>

A solution that could centralize the set method call is to write a DIC tag and a compiler pass to handle it and tag all repository services.


This is a YAML version of Aldo's answer, just in case you are using YAML configurations instead of XML

your_namespace.repository.repos_name:    class: %your_namespace.repository.repos_name%    factory: ["@doctrine", getRepository]    arguments:        - entity_name        - entity_manager_name    calls:        - [setContainer, ["@service_container"]]

And prior to version 2.8:

your_namespace.repository.repos_name:    class: %your_namespace.repository.repos_name%    factory_service: doctrine    factory_method: getRepository    arguments:        - entity_name        - entity_manager_name    calls:        - [setContainer, [@service_container]]

Also, as a note, entity_manager_name is an optional parameter. I want the default for my particular use, so I just left it blank (just in case I ever rename the default manager).


If you use a custom EntityManager you could override the getRepository method. Since this doesn't involve the loadClassMetadata event, you won't run into an infinite loop.

You would first have to pass the dependency to your custom EntityManager, and then you'd pass it to the repository object using setter injection.

I answered how to use a custom EntityManager here, but I'll replicate the answer below:

1 - Override the doctrine.orm.entity_manager.class parameter to point to your custom entity manager (which should extend Doctrine\ORM\EntityManager.)

2 - Your custom entity manager must override the create method so that it returns an instance of your class. See my example below, and note the last line regarding MyEntityManager:

public static function create($conn, Configuration $config, EventManager $eventManager = null) {        if (!$config->getMetadataDriverImpl()) {            throw ORMException::missingMappingDriverImpl();        }        if (is_array($conn)) {            $conn = \Doctrine\DBAL\DriverManager::getConnection($conn, $config, ($eventManager ? : new EventManager()));        } else if ($conn instanceof Connection) {            if ($eventManager !== null && $conn->getEventManager() !== $eventManager) {                throw ORMException::mismatchedEventManager();            }        } else {            throw new \InvalidArgumentException("Invalid argument: " . $conn);        }        // This is where you return an instance of your custom class!        return new MyEntityManager($conn, $config, $conn->getEventManager());    }

You'll also need to use the following in your class:

use Doctrine\ORM\EntityManager;use Doctrine\ORM\Configuration;use Doctrine\ORM\ORMException;use Doctrine\Common\EventManager;use Doctrine\DBAL\Connection;

Edit

Since the default entity manager is created from the create method, you can't simply inject a service into it. But since you're making a custom entity manager, you can wire it up to the service container and inject whatever dependencies you need.

Then from within the overridden getRepository method you could do something like
$repository->setFoo($this->foo). That's a very simple example - you may want to first check if $repository has a setFoo method before calling it. The implementation is up to you, but this shows how to use setter injection for a repository.