E
E
Elena2015-07-09 08:37:16
symfony
Elena, 2015-07-09 08:37:16

How to call EntityManager in a controller which is defined as a service?

In a regular service, I passed the @doctrine.orm.entity_manager argument to the constructor and everything worked.
Now it took some methods in the controller to reuse, and I'm doing something wrong.
The documentation symfony.com/doc/current/cookbook/controller/servic... says
"Simply inject doctrine instead of fetching it from the container" on this. And there is no example.
How to do it right?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2015-07-09
Protko @Fesor

Now it took some methods in the controller to reuse

Congratulations, you are on your way to application level services and thin controllers.
instead of making controllers as services - just make services and there will be no problems.
And there are examples there: symfony.com/doc/current/book/service_container.html - read it.

D
Denis, 2015-07-09
@prototype_denis

Read what Sergei Protko suggested to you .

services:
    app.super_puper_controller:
        class: AppBundle\Controller\BlackMagicController
        calls:
            - [setContainer, ["@services_container"]]
            - [setOtherMethod, ["@router", "%my_parameter%"]]

As a result, when you initialize your controller, Symfony will go through the list of calls and call each method with parameters.
(PSY. It is better to explicitly pass arguments to the service. That is, as you did with regular services)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question