Using the ServiceLocator in ZF2 with Doctrine 2 from/in a Custom Class?

我怕爱的太早我们不能终老 提交于 2019-12-12 05:42:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!