How to use Symfony autowiring with multiple entity managers

Vivien

I would like to use the autowiring in a service that use 2 different entity manager. How to achieve something like that ?

use Doctrine\ORM\EntityManager;
class TestService
{
    public function __construct(EntityManager $emA, EntityManager $emB)
    {
    }
}

My service.yml file use to be configured like that :

    app.testservice:
        class: App\Services\TestService
        arguments:
            - "@doctrine.orm.default_entity_manager"
            - "@doctrine.orm.secondary_entity_manager"
emix

Dylan's answer violates the Demeter's Law principle. It's very easy and elegant since Symfony 3.4, meet Local service binding:

services:
  _defaults:
    bind:
      $emA: "@doctrine.orm.default_entity_manager"
      $emB: "@doctrine.orm.secondary_entity_manager"

Then in your service the autoloading will do the hard work for you:

class TestService
{
    public function __construct(EntityManager $emA, EntityManager $emB)
    {
        …
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

symfony doctrine migrations with multiple entity managers

How do I set up Doctrine 2 with multiple Entity Managers in one Project? (without Symfony/Zend)

Symfony2 Multiple databases configuration/connections and entity managers

Symfony2 doctrine:schema:update with multiple Entity Managers

Single or Multiple Entity managers

AliceBundle with multiple entity managers?

Resolve Target Entity with multiple entity managers

Cli-Config Helper -Multiple Entity Managers

How to use Doctrine Entity Listener with Symfony 2.4?

Symfony 4.4 how to configure controllers without autowiring

how to use an attribute of an entity that has a manytomany relationship with another entity in symfony

Manage transactions with multiple datasource, entity managers for same application code

How use multiple @Embedded in same entity?

How to use multiple endpoints for Symfony NexySlackBundle

Symfony/Console: How to use multiple progress bars?

How to create a form with multiple rows of one entity in Symfony2

How to use AutoWiring when looping through Subclasses?

How to use a Managers to save data in Django

How to use custom managers in reverse relations?

how to use two entity manager for one direction in symfony 4

Symfony: How to use a form filter with an field that does not exist in an entity?

How to use Entity, Form, Controller in a service for a websocket symfony app

Multiple entity in one form Symfony

Symfony - Form with multiple entity objects

How does Symfony's autowiring work behind the scenes?

Symfony autowiring monolog channels

Symfony 4 - Autowiring not working

Spring - Is it possible to use multiple transaction managers in the same application?

Should I pass doctrine Registry or specific repositories, entity managers to DI container in symfony2?