1

I have defined two different entity managers in doctrine.yaml

entity_managers:
            EM1:
                naming_strategy: doctrine.orm.naming_strategy.underscore
                mappings:
                    App\EM1:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/BC1/Domain/Entity'
                        prefix: 'BC1\Domain\Entity'
                        alias:  App\EM1
            EM2:
                naming_strategy: doctrine.orm.naming_strategy.underscore
                mappings:
                    App\EM2:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/BC2/Domain/Entity'
                        prefix: 'BC2\Domain\Entity'
                        alias:  App\EM2

and I am injecting a Doctrine EntityManagerInterface in my service:

service.yaml

    DoctrineRepository:
        class: BC\Infrastructure\Persistence\DoctrineRepository
        arguments:
            - "@Doctrine\\ORM\\EntityManagerInterface"

DoctrineRepository.php

    public function __construct(
        EntityManagerInterface $client
    ) {
        $this->entityManager = $client;
    }

I want to be able to select which Entity Manager previously defined (EM1, EM2) I want to use when using DoctrineRepository. Idealy it would be something like:

$this->entityManager->useEntityManager('EM2');

Do I need to inject another service instead of a Doctrine\ORM\EntityManagerInterface? I have debugged the entity managers load and I have found that it automatically assigns the first one when generating cache:

cache/dev/getDoctrineRepositoryService.php:

return $this->privates['DoctrineRepository'] = new BC\Infrastructure\Persistence\DoctrineRepository(($this->services['doctrine.orm.EM1_entity_manager'] ?? $this->load('getDoctrine_Orm_EM1EntityManagerService.php')));

Any help would be apreciated, thanks.

2 Answers 2

2

I think I found another solution. Instead of injecting an EntityManagerInterface, I have injected @Doctrine itself (it injects a Doctrine\Bundle\DoctrineBundle\Registry object), and from that object you can do a $object->getManager($manager) so select the entity manager you want

Sign up to request clarification or add additional context in comments.

Comments

0

Since you have two services that implements the same interface you need to help autowiring of Symfony.

Like explained in the docs:

you can create a normal alias from the TransformerInterface interface to Rot13Transformer, and then create a named autowiring alias from a special string containing the interface followed by a variable name matching the one you use when doing the injection

In your case

    # the ``App\EM2`` service will be
    # injected when an ``Doctrine\ORM\EntityManagerInterface``
    # type-hint for a ``$em2Manager`` argument is detected.
    Doctrine\ORM\EntityManagerInterface $em2Manager: '@App\EM2'

    # If the argument used for injection does not match, but the
    # type-hint still matches, the ``Doctrine\ORM\EntityManagerInterface``
    # service will be injected.
    Doctrine\ORM\EntityManagerInterface: '@App\EM1'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.