如何在zf2插件中调用doctrine.entitymanager.orm_default

时间:2015-08-27 19:36:21

标签: php doctrine-orm zend-framework2

我想在我的自定义插件中使用我的实体。所以,我按顺序做:

1)在Module \ src \ Plugin \ Plugin.php

中声明了我的插件
calc(100vw - 200px)

2)创建了我的工厂:

  namespace Application\Plugin;

  use Zend\Mvc\Controller\Plugin\AbstractPlugin;
  use Doctrine\ORM\EntityManager;
  use User\Entity\UserProfile;
  use Zend\ServiceManager\ServiceManager;

  class AuthenticationPlugin extends AbstractPlugin {
       protected $entityManager;
       protected $serviceManager;

  public function setServiceManager(ServiceManager $locator) {
       $this->serviceManager = $locator;
  }

  public function getServiceManager() {
       return $this->serviceManager;
  }

  public function getEntityManager() {
       $userEntityFactory = new \Application\Factory\UserEntityFactory();

       $this->entityManager = $userEntityFactory->createService($this->getServiceManager());

       return $this->entityManager;
  }

  public function someAction($user_email) {
       $user = $this->getEntityManager()->getRepository('User\Entity\User')->findBy(array('email'=>$user_email));
  }
}

3)在module.config.php中定义它:

 namespace User\Factory;

 use Zend\ServiceManager\FactoryInterface;
 use Zend\ServiceManager\ServiceLocatorInterface;

 class UserEntityFactory implements FactoryInterface {

     public function createService(ServiceLocatorInterface $serviceLocator) {
        return $serviceLocator->get('doctrine.entitymanager.orm_default');
     }    
 }

4)将ServiceLocator发送到Module.php中的插件:

'service_manager' => array(
    'factories' => array(
        'UserEntityFactory' => 'Application\Factory\UserEntityFactory',
    ),
),
'controller_plugins' => array(
    'invokables' => array(
        'AuthenticationPlugin' => 'Application\Plugin\AuthenticationPlugin',
    )
),

5)...并在onBootstrap中调用它:

public function getServiceConfig() {
        return array(
            'factories' => array(
                'AuthenticationPlugin' => function($sm) {
                    $locator = $sm->getServiceLocator();
                    $instance = new \Application\Plugin\AuthenticationPlugin();
                    $instance->setServiceManager($locator);

                    return $instance;
                },
            ),
        );
    }

但我收到错误,插件中的$ locator为空...我很困惑,我确信我做错了什么......或者全部。如果有人分享经验或会显示行动的顺序,我会很高兴。感谢。

1 个答案:

答案 0 :(得分:0)

您无需将整个服务管理器对象注入到插件类中。

您只需要注入User \ Entity \ User存储库对象,这似乎是插件类中唯一需要的依赖项。

您应该通过工厂将其传递给插件类的构造函数:

public function getServiceConfig() {
        return array(
            'factories' => array(
                'AuthenticationPlugin' => function($sm) {
                    return new \Application\Plugin\AuthenticationPlugin($sm->get('doctrine.entitymanager.orm_default')->getRepository('User\Entity\User'));
                },
            ),
        );
    }

在你的插件类中:

    class AuthenticationPlugin extends AbstractPlugin {

    private $userRepository;

    public function __construct(\User\Entity\User $userRepository){

        $this->userRepository=$userRepository;

    }

    public function someAction($user_email) {
       $user = $this->userRepository->findBy(array('email'=>$user_email));
    }
                                                      }

当您通过module.php配置插件时,您不需要在配置文件中将插件声明为可调用的。因此,从module.config.php中删除以下行

'AuthenticationPlugin' => 'Application\Plugin\AuthenticationPlugin'

作为旁注,在module.php或module.config文件中声明服务/插件之间有各种利弊。这虽然不是问题所以我不会在这里详细说明。