Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to getEntityManager() from outside model/controller?

I'm trying to use Symfony 2 with Doctrine 2. But I have following problem: after creating class that in fact is neither controller nor model, I wanted to use there getEntityManager(). But how can I get this?

Thx in advance.

like image 581
pbuchman Avatar asked Aug 07 '26 15:08

pbuchman


1 Answers

You need to define this class a a service and then pass entity manager as an argument (either inside constructor or via setter).

For more info take a look at Service Container documentation.

Something like this:

services:
    my_hello_class:
        class:        Acme\HelloBundle\HelloClass
        arguments:    ["@doctrine.orm.entity_manager"]

And then the class would look like this:

// ....

class HelloClass
{
    private $em;

    public function __construct(\Doctrine\ORM\EntityManager $em)
    {
        $this->em = $em; 

        // ....
    }   

}
like image 95
Inoryy Avatar answered Aug 10 '26 07:08

Inoryy