we can access entity manager within controller using $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
but how can we access entity manager singleton instance in rest of the project in Zendframework 2.
we can access entity manager within controller using $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
but how can we access entity manager singleton instance in rest of the project in Zendframework 2.
The 'right' way to do it is use a factory to inject the entity manager into any classes that need it. Classes, other than factories, shouldn't really be aware of the ServiceLocator. So, your module config would look like this:
Then your factory class would look something like this:
Follow the same pattern for any other classes that need to consume the entity manager.
If, you have lots and lots of classes that consume the entity manager, you might want to consider adding your own Initalizer to the SerivceManager that will inject the entity manager without the need for a factory.