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.
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;
// ....
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With