Skip to main content
edited body
Source Link
Touki
  • 7.5k
  • 3
  • 44
  • 66

Answer at 18/02/13

Here is a way to pass a specific entity manager from services.yml
Doctrine generates a new service name relative to their names.

Example:

@doctrine.orm.default_entity_manager 

In this case, it generates 2 others entity manager

@doctrine.orm.main_entity_manager @doctrine.orm.sub_entity_manager 

The argument passed is a Doctrine\ORM\EntityManager object

In my case:

services.yml

submanager: arguments: [ @doctrine.orm.sub_entity_manager ] 

Update 22/08/13

An alternative to this would be to directly give the repository instead of the manager.

To do such, you must create a service which will hold the repository.

services.yml

services: acme.foo_repository: class: Doctrine\Common\Persistence\ObjectRepository factory_service: doctrine.orm.main_entity_manager factory_method: getRepository arguments: - "AcmeMainBundle:Foo" 

We let doctrine generate the given repository.
Then we can inject it in another service

services: mainmanager: class: Acme\MainBundle\MainManager arguments: - @acme.foo_repository 

Answer at 18/02/13

Here is a way to pass a specific entity manager from services.yml
Doctrine generates a new service name relative to their names.

Example:

@doctrine.orm.default_entity_manager 

In this case, it generates 2 others entity manager

@doctrine.orm.main_entity_manager @doctrine.orm.sub_entity_manager 

The argument passed is a Doctrine\ORM\EntityManager object

In my case:

services.yml

submanager: arguments: [ @doctrine.orm.sub_entity_manager ] 

Update 22/08/13

An alternative to this would be to directly give the repository instead of the manager.

To do such, you must create a service which will hold the repository.

services.yml

services: acme.foo_repository: class: Doctrine\Common\Persistence\ObjectRepository factory_service: doctrine.orm.main_entity_manager factory_method: getRepository arguments: - "AcmeMainBundle:Foo" 

We let doctrine generate the given repository.
Then we can inject it in another service

services: mainmanager: class: Acme\MainBundle\MainManager arguments: - @acme.foo_repository 

Answer at 18/02/13

Here is a way to pass a specific entity manager from services.yml
Doctrine generates a new service name relative to their names.

Example:

@doctrine.orm.default_entity_manager 

In this case, it generates 2 others entity manager

@doctrine.orm.main_entity_manager @doctrine.orm.sub_entity_manager 

The argument passed is a Doctrine\ORM\EntityManager object

In my case:

services.yml

submanager: arguments: [ @doctrine.orm.sub_entity_manager ] 

Answer at 18/02/13

Here is a way to pass a specific entity manager from services.yml
Doctrine generates a new service name relative to their names.

Example:

@doctrine.orm.default_entity_manager 

In this case, it generates 2 others entity manager

@doctrine.orm.main_entity_manager @doctrine.orm.sub_entity_manager 

The argument passed is a Doctrine\ORM\EntityManager object

In my case:

services.yml

submanager: arguments: [ @doctrine.orm.sub_entity_manager ] 

Update 22/08/13

An alternative to this would be to directly give the repository instead of the manager.

To do such, you must create a service which will hold the repository.

services.yml

services: acme.foo_repository: class: Doctrine\Common\Persistence\ObjectRepository factory_service: doctrine.orm.main_entity_manager factory_method: getRepository arguments: - "AcmeMainBundle:Foo" 

We let doctrine generate the given repository.
Then we can inject it in another service

services: mainmanager: class: Acme\MainBundle\MainManager arguments: - @acme.foo_repository 
added 39 characters in body
Source Link
Touki
  • 7.5k
  • 3
  • 44
  • 66

Update 22/08/13

An alternative to this would be to directly give the repository instead of the manager.

To do such, you must create a service which will hold the repository.

services.yml

services:   acme.foo_repository:   class: Doctrine\Common\Persistence\ObjectRepository   factory_service: doctrine.orm.main_entity_manager   factory_method: getRepository   arguments:   - "AcmeMainBundle:Foo" 

We let doctrine generate the given repository.
Then we can inject it in another service

services:   mainmanager:   class: Acme\MainBundle\MainManager   arguments:   - @acme.foo_repository 

Answer at 18/02/13

I finally foundHere is a way to pass a specific entity manager from services.yml
Doctrine generates a new service name relative to their names.

Example:

@doctrine.orm.default_entity_manager 

In this case, it generates 2 others entity manager

@doctrine.orm.main_entity_manager @doctrine.orm.sub_entity_manager 

The argument passed is a Doctrine\ORM\EntityManager object

In my case:

services.yml

submanager:   arguments: [ @doctrine.orm.sub_entity_manager ] 

Update 22/08/13

An alternative to this would be to directly give the repository instead of the manager.

To do such, must create a service which will hold the repository

services.yml

services: acme.foo_repository: class: Doctrine\Common\Persistence\ObjectRepository factory_service: doctrine.orm.main_entity_manager factory_method: getRepository arguments: - "AcmeMainBundle:Foo" 

We let doctrine generate the given repository.
Then we can inject it in another service

services: mainmanager: class: Acme\MainBundle\MainManager arguments: - @acme.foo_repository 

Answer at 18/02/13

I finally found a way to pass a specific entity manager from services.yml
Doctrine generates a new service name relative to their names.

Example:

@doctrine.orm.default_entity_manager 

In this case, it generates 2 others entity manager

@doctrine.orm.main_entity_manager @doctrine.orm.sub_entity_manager 

The argument passed is a Doctrine\ORM\EntityManager object

In my case:

services.yml

submanager: arguments: [ @doctrine.orm.sub_entity_manager ] 

Update 22/08/13

An alternative to this would be to directly give the repository instead of the manager.

To do such, you must create a service which will hold the repository.

services.yml

services:   acme.foo_repository:   class: Doctrine\Common\Persistence\ObjectRepository   factory_service: doctrine.orm.main_entity_manager   factory_method: getRepository   arguments:   - "AcmeMainBundle:Foo" 

We let doctrine generate the given repository.
Then we can inject it in another service

services:   mainmanager:   class: Acme\MainBundle\MainManager   arguments:   - @acme.foo_repository 

Answer at 18/02/13

Here is a way to pass a specific entity manager from services.yml
Doctrine generates a new service name relative to their names.

Example:

@doctrine.orm.default_entity_manager 

In this case, it generates 2 others entity manager

@doctrine.orm.main_entity_manager @doctrine.orm.sub_entity_manager 

The argument passed is a Doctrine\ORM\EntityManager object

In my case:

services.yml

submanager:   arguments: [ @doctrine.orm.sub_entity_manager ] 
deleted 436 characters in body
Source Link
Touki
  • 7.5k
  • 3
  • 44
  • 66

Update 1822/0208/13

I finally found a wayAn alternative to pass a specific entity manager from services.yml
Doctrine generates a new service name relativethis would be to directly give the connectionsrepository instead of the manager.

Example:

@doctrine.orm.default_entity_manager 

In this case, since the connection needed is called starwebTo do such, the service is called

@doctrine.orm.starweb_entity_manager 

The argument passed ismust create a Doctrine\ORM\EntityManager object

In my case:service which will hold the repository

VManagerservices.yml

use Doctrine\ORM\EntityManager; services: class VManager { acme.foo_repository: protectedclass: $_em; Doctrine\Common\Persistence\ObjectRepository   public function __construct(EntityManagerfactory_service: $em)doctrine.orm.main_entity_manager { factory_method: getRepository  arguments:  $this->_em = $em;  - } }"AcmeMainBundle:Foo" 

services.yml We let doctrine generate the given repository.
Then we can inject it in another service

vmanagerservices: mainmanager: class: Acme\MainBundle\MainManager   arguments:   [@doctrine.orm.starweb_entity_manager, .. - @acme.]foo_repository 

Old answer
Please note that this solution breaks the Law of DemeterAnswer at 18/02/13

There is noI finally found a way to change the connectionpass a specific entity manager from an EntityManager object.services.yml
It seems the best way isDoctrine generates a new service name relative to use it that way:their names.

VManager Example:

use Doctrine\Bundle\DoctrineBundle\Registry; class VManager { protected $_em; public function __construct(Registry $doctrine) { $this->_em = $doctrine->getEntityManager('starweb'); } }@doctrine.orm.default_entity_manager 

starweb stands for the entity_manager name in config.yml

services.yml In this case, it generates 2 others entity manager

vmanager: class: ST\CoreBundle\Manager\VManager arguments: [@doctrine, @doctrine.orm.main_entity_manager @doctrine.]orm.sub_entity_manager 

This works for Symfony 2.1
You'd get an error on Symfony 2.0 due to the namespace change of DoctrineBundle.
This can be fixed by changing theThe argument passed is a useDoctrine\ORM\EntityManager statement by object

In my case:

services.yml

usesubmanager:  Symfony\Bundle\DoctrineBundle\Registry; arguments: [ @doctrine.orm.sub_entity_manager ] 

 

Update 18/02/13

I finally found a way to pass a specific entity manager from services.yml
Doctrine generates a new service name relative to the connections.

Example:

@doctrine.orm.default_entity_manager 

In this case, since the connection needed is called starweb, the service is called

@doctrine.orm.starweb_entity_manager 

The argument passed is a Doctrine\ORM\EntityManager object

In my case:

VManager

use Doctrine\ORM\EntityManager;  class VManager { protected $_em;    public function __construct(EntityManager $em) {  $this->_em = $em;  } } 

services.yml

vmanager: arguments: [@doctrine.orm.starweb_entity_manager, ...] 

Old answer
Please note that this solution breaks the Law of Demeter

There is no way to change the connection from an EntityManager object.
It seems the best way is to use it that way:

VManager

use Doctrine\Bundle\DoctrineBundle\Registry; class VManager { protected $_em; public function __construct(Registry $doctrine) { $this->_em = $doctrine->getEntityManager('starweb'); } } 

starweb stands for the entity_manager name in config.yml

services.yml

vmanager: class: ST\CoreBundle\Manager\VManager arguments: [@doctrine, ...] 

This works for Symfony 2.1
You'd get an error on Symfony 2.0 due to the namespace change of DoctrineBundle.
This can be fixed by changing the use statement by :

use Symfony\Bundle\DoctrineBundle\Registry; 

 

Update 22/08/13

An alternative to this would be to directly give the repository instead of the manager.

To do such, must create a service which will hold the repository

services.yml

services:  acme.foo_repository: class: Doctrine\Common\Persistence\ObjectRepository factory_service: doctrine.orm.main_entity_manager factory_method: getRepository  arguments:  - "AcmeMainBundle:Foo" 

We let doctrine generate the given repository.
Then we can inject it in another service

services: mainmanager: class: Acme\MainBundle\MainManager   arguments:    - @acme.foo_repository 

Answer at 18/02/13

I finally found a way to pass a specific entity manager from services.yml
Doctrine generates a new service name relative to their names.

Example:

@doctrine.orm.default_entity_manager 

In this case, it generates 2 others entity manager

@doctrine.orm.main_entity_manager @doctrine.orm.sub_entity_manager 

The argument passed is a Doctrine\ORM\EntityManager object

In my case:

services.yml

submanager:   arguments: [ @doctrine.orm.sub_entity_manager ] 
added 820 characters in body
Source Link
Touki
  • 7.5k
  • 3
  • 44
  • 66
Loading
Source Link
Touki
  • 7.5k
  • 3
  • 44
  • 66
Loading