1
votes

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

2 Answers

1
votes

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

0
votes

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'