0

I need to have access to my database anywhere in my application. I have a lot of custom class and object and they need to have access to my database. I tried to extends Controller, or implement ContainerAwareInterface in one of my class, but without success. I want just used the doctrine manager like this any where in my application :

$em = $this->getDoctrine()->getManager();
$em->getRepository('MyTestBundle:Series')->findAllSeries();

I have tried to use services but I don't know how I can use it in all my classes after.

Etienne
  • 408
  • 2
  • 9
  • 28

1 Answers1

1

You can inject entity manager in Services (your custom classes) as a argument. Check this answer given by Chedwick Meyer.

And for controller I suggest you should create one base controller which extend controller and set Entity Manager variable in base controller. Extend this base controller in all your controller then you can use all properties and methods of this controller. You can define any common functions which you want to access in all controllers in base controller.

Community
  • 1
  • 1
Maya Shah
  • 950
  • 7
  • 17