【问题标题】:ZF2 and EntityManager (Doctrine)ZF2 和 EntityManager(学说)
【发布时间】:2012-10-25 09:10:44
【问题描述】:

我有问题。我尝试在没有控制器的情况下获取实体管理器,但我没有找到办法。 这时候,我得到的Entity-Manager是这样的:

(Controller)
public function getEntityManager()
{
    if (null === $this->_em) {
        $this->_em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
    }
    return $this->_em;
}

(Plugin)
public function getEntityManager()
{
    if($this->_em == null){
        $this->_em = $this->getController()->getServiceLocator()->get('doctrine.entitymanager.orm_default');
    }
    return $this->_em;
}

你看,我总是需要一个控制器。但是,如果我需要模型中的 EntityManager,我就有问题了。我可以给模型控制器,但我认为这确实是一个糟糕的方法。

你有什么想法在没有控制器的情况下获得 EntityManager 吗?

【问题讨论】:

    标签: doctrine zend-framework2 entitymanager


    【解决方案1】:

    我处理 Doctrine 的方式是通过服务,我这样做如下:

    //some Controller
    public function someAction()
    {
        $service = $this->getServiceLocator()->get('my_entity_service');
        return new ViewModel(array(
            'entities' => $service->findAll()
        ));
    }
    

    Service->findAll() 看起来像这样:

    public function findAll()
    {
        return $this->getEntityRepository()->findAll();
    }
    

    现在我们需要定义my_entity_service。我在我的Module.php 中这样做了

    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'my_entity_service' => 'Namespace\Factory\MyServiceFactory'
            )
        );
    }
    

    接下来我创建工厂(注意:这也可以通过 Module.php 中的匿名函数来完成)

    <?php
    namespace Namespace\Factory;
    
    use Zend\ServiceManager\ServiceLocatorInterface;
    use Zend\ServiceManager\FactoryInterface;
    use Namespace\Model\MyModel;
    
    class MyServiceFactory implements FactoryInterface
    {
        /**
         * Create service
         *
         * @param ServiceLocatorInterface $serviceLocator
         * @return mixed
         */
        public function createService(ServiceLocatorInterface $serviceLocator)
        {
            $myModel= new MyModel();
            $myModel->setEntityManager($serviceLocator->get('Doctrine\ORM\EntityManager'));
    
            return $myModel;
        }    
    }
    

    现在有很多东西要咀嚼:D 我完全明白了。这里发生的事情实际上非常简单。您无需创建模型并以某种方式访问​​ EntityManager,而是调用 ZF2 的 ServiceManager 为您创建模型并将 EntityManager 注入其中。

    如果这仍然让您感到困惑,我很乐意尝试更好地解释自己。但是,为了进一步澄清,我想了解您的用例。即:你需要 EntityManager 做什么,或者你到底在哪里需要它。

    此代码示例超出问题范围

    只是给你一个我通过 ServiceFactories 用表单做的事情的例子:

    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $form = new ReferenzwertForm();
        $form->setHydrator(new DoctrineEntity($serviceLocator->get('Doctrine\ORM\EntityManager')))
             ->setObject(new Referenzwert())
             ->setInputFilter(new ReferenzwertFilter())
             ->setAttribute('method', 'post');
    
        return $form;
    }
    

    【讨论】:

      【解决方案2】:

      您真正的问题是“如何在我自己的类中获取 ServiceManager 的实例

      为此,请查看文档:(页面底部http://zf2.readthedocs.org/en/latest/modules/zend.service-manager.quick-start.html

      默认情况下,Zend Framework MVC 注册一个初始化器,它将 注入 ServiceManager 实例,它是 Zend\ServiceManager\ServiceLocatorInterface,进入任何类 实现 Zend\ServiceManager\ServiceLocatorAwareInterface。一种 简单的实现如下所示。

      所以在你的类中实现 ServiceLocatorInterface,然后你可以在你的类中调用:

      $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
      

      或您已注册的任何其他服务。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-14
        • 1970-01-01
        • 2013-02-04
        • 2014-07-19
        • 2013-02-05
        • 1970-01-01
        相关资源
        最近更新 更多