0
votes

I don´t know how to do this,

I create a service in Symfony2 and i need to this service use request and doctrine entitymanager service

In services.yml i add these lines:

    logdb:
      class:     %logdb.class%
      arguments: ['@doctrine.orm.entity_manager','@request_injector']

My service class:

class LogDB {

protected $em;
protected $request;

public function __construct(EntityManager $em, Request $request){

    $this->em= $em;
    $this->request= $request;
}

public function saveLog(){

}

}

Well, in saveLog() i need to acces to entitymanager but from a different manager of default, i mean entity manager i am injecting with '@doctrine.orm.entity_manager' is default.

In a controller i can do this:

$em = $this->getDoctrine()->getManager($this->getRequest()->get('shop'));

I use differents databases for every shop, that i choose by url param. Ask is, how can i acces to a custom entitymanager from a service?

injecting container is a bad solution...

maybe i need to pass entitymanager as argument in every service method from controller i don´t know

1

1 Answers

3
votes

Then you are injecting the wrong Object. Why not inject doctrine to the service?

services:
    logdb:
       class:     %logdb.class%
       arguments: ['@doctrine','@request_injector']
                   # ^^^^^^^^^^^^^- not the entity_manager

Then you can call this in your service:

$em = $this->doctrine->getManager($this->request);