问题
i have a little problem using doctrine 2 in Zend Framework 2 i have custome class that i use to manipulate doctrine generated model (basically to inject data and populate), to make that work i need the entity manager which is available through the service manager as indicated in Jason Grimes tutorial here http://www.jasongrimes.org/2012/01/using-doctrine-2-in-zend-framework-2/.
In his tutorial it works (i tested it) as the ServiceLocator is called from a controller class, but for the application i am writing i have to use custom (non controller) classes to interact with the entities. How do i achieve this? Using the servicelocator from a custom class that is not a controller? Thank you all in advance
回答1:
You need to do two steps
Implement
Zend\ServiceManager\ServiceLocatorAwareInterface
in your custom class. This allows to the Framework to inject the Service Locator for you.Convert your custom class to a service and retrieve it using Service Manager. This component will check if the class implement ServiceLocatorAwareInterface and the inject the ServiceLocator before returning to you the instance
来源:https://stackoverflow.com/questions/13506059/using-the-servicelocator-in-zf2-with-doctrine-2-from-in-a-custom-class