Uvigii
Uvigii

Reputation: 49

Symfony 5.2 Multiple Entity Managers strange behaviour

I'm trying to use multiple entity managers as described here. Example code in controller where $shop variable holds the name of the manager:

    $rp = new RemoteProduct();
    $rp->setProducts(array(2000,2001,2002));
 
    $em = $this->getDoctrine()->getManager($shop);
    $em->persist($rp);
    $em->flush();

    $repo = $em->getRepository(RemoteProduct::class, $shop);
    $repo->findAll();

Generates queries to both managers. Flush as a transaction in the correct DB, but select queries are executed in the default DB instead. Any ideas?

Queries to both managers

doctrine:
dbal:
    default_connection: shop1
    connections:
        shop1:                
            url: '%env(resolve:DATABASE_URL)%'
            mapping_types:
              enum: string              
        store2:                
            url: '%env(resolve:DATABASE_URL_STORE)%'
            mapping_types:
              enum: string
orm:           
    auto_generate_proxy_classes: true
    default_entity_manager: shop1
    entity_managers:
        shop1:
            connection: shop1
            mappings:
                shop1:
                    is_bundle: false
                    type: annotation
                    dir: '%kernel.project_dir%/src/Entity'
                    prefix: 'App\Entity'
                    alias: shop1
        store2:
            connection: store2
            mappings:
                store2:
                    is_bundle: false
                    type: annotation
                    dir: '%kernel.project_dir%/src/Entity'
                    prefix: 'App\Entity'
                    alias: store2  

Upvotes: 0

Views: 51

Answers (1)

Uvigii
Uvigii

Reputation: 49

It happens that big fat warning at the end of the documentation was ignored by me. Do not extend repository class from ServiceEntityRepository

In order to fix this situation, extend EntityRepository instead and no longer rely on autowiring:

Upvotes: 1

Related Questions