Symfony2: get Doctrine in a generic PHP class

前端 未结 2 828
抹茶落季
抹茶落季 2021-01-06 08:13

In a Symfony2 project, when you use a Controller, you can access Doctrine by calling getDoctrine() on this, i.e.:

2条回答
  •  灰色年华
    2021-01-06 08:20

    After checking the symfony2 docs i figured out how to pass your service in a custom method to break the default behavior.

    Rewrite your configs like this:

    services:
    my_mailer:
        class: Path/To/GenericClass
        calls:
             - [anotherMethodName, [doctrine.orm.entity_manager]]
    

    So, the Service is now available in your other method.

    public function anotherMethodName($entityManager)
    {
        // your magic
    }
    

    The Answer from Ondrej is absolutely correct, I just wanted to add this piece of the puzzle to this thread.

提交回复
热议问题