在ZF2中使用ServiceLocator与自定义类中的/来自Doctrine 2? [英] Using the ServiceLocator in ZF2 with Doctrine 2 from/in a Custom Class?

查看:185
本文介绍了在ZF2中使用ServiceLocator与自定义类中的/来自Doctrine 2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Zend Framework 2中使用doctrine 2有一些问题我有用于操纵原则生成模型(基本上是注入数据和填充)的custome类,为了使这项工作需要通过实体管理器可用的实体管理器服务经理,如Jason Grimes教程中所示。 http ://www.jasongrimes.org/2012/01/using-doctrine-2-in-zend-framework-2/



在他的教程中它工作(我测试它),因为ServiceLocator从控制器类调用,但对于我正在编写的应用程序,我必须使用自定义(非控制器)类与实体进行交互。我该如何实现?使用不是控制器的自定义类的servicelocator?谢谢所有提前

解决方案

您需要执行两个步骤




  • 在您的自定义类中实现 Zend\ServiceManager\ServiceLocatorAwareInterface 。这允许框架为您注入服务定位器。


  • 将自定义类转换为服务,并使用服务管理器进行检索。此组件将检查该类是否实现ServiceLocatorAwareInterface,并在返回到实例之前注入ServiceLocator



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

解决方案

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

这篇关于在ZF2中使用ServiceLocator与自定义类中的/来自Doctrine 2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆