在ZF2中创建具有依赖关系(依赖注入)的doctrine存储库

Wil*_*ilt 4 php doctrine dependency-injection doctrine-orm zend-framework2

我想创建一个具有硬依赖关系的存储库.我在Jurian Sluisman发现了这篇博文,但他建议从服务管理器获取存储库,并将其注入服务中.

如果我能够通过使用方法从我的EntityManagerObjectManager实例中获取注入依赖项的自定义存储库会好得多getRepository:

$objectManager->getRepository('My\Entity\Class');
Run Code Online (Sandbox Code Playgroud)

我怎样才能在我的存储库中使用构造函数注入,并且仍然可以ObjectManager直接从getRepository方法中获取它们?

Wil*_*ilt 12

Doctrine使用工厂类Doctrine\ORM\EntityManagerInterface\DefaultRepositoryFactory来创建存储库实例.如果没有定制出厂设置创建此默认出厂这里的getRepositoryFactory方法Doctrine\ORM\Configuration.

通过定义自定义,repository_factory我们可以覆盖此默认工厂类,并将自定义逻辑添加到将注入硬依赖项的工厂:

为了说明如何执行此操作,我将展示一个示例,其中存储库工厂类ServiceLocator通过构造函数注入创建依赖于实例的存储库.

1)创建一个实现doctrine RepositoryFactory接口的自定义工厂类

该类看起来与教义DefaultRepositoryFactory非常相似.

<?php

namespace My\ORM\Repository;

use Doctrine\Common\Persistence\ObjectRepository;    
use Doctrine\ORM\Repository\RepositoryFactory;
use Doctrine\ORM\EntityManagerInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait;
use Zend\ServiceManager\ServiceLocatorInterface;

class CustomRepositoryFactory implements RepositoryFactory, ServiceLocatorAwareInterface
{
    use ServiceLocatorAwareTrait;

    /**
     * @var ObjectRepository[]
     */
    private $repositoryList = array();

    /**
     * @var ServiceLocator
     */
    protected $serviceLocator;

    /**
     * @param ServiceLocatorInterface $serviceLocator
     */
    public function __construct(ServiceLocatorInterface $serviceLocator)
    {
        $this->serviceLocator = $serviceLocator;
    }

    /**
     * {@inheritdoc}
     */
    public function getRepository(EntityManagerInterface $entityManager, $entityName)
    {
        $repositoryHash = $entityManager->getClassMetadata($entityName)->getName() . spl_object_hash($entityManager);

        if (isset($this->repositoryList[$repositoryHash])) {
            return $this->repositoryList[$repositoryHash];
        }

        return $this->repositoryList[$repositoryHash] = $this->createRepository($entityManager, $entityName);
    }

    /**
     * @param EntityManagerInterface $entityManager The EntityManager instance.
     * @param string                 $entityName    The name of the entity.
     * @return ObjectRepository
     */
    private function createRepository(EntityManagerInterface $entityManager, $entityName)
    {
        /* @var $metadata \Doctrine\ORM\Mapping\ClassMetadata */
        $metadata            = $entityManager->getClassMetadata($entityName);
        $repositoryClassName = $metadata->customRepositoryClassName
            ?: $entityManager->getConfiguration()->getDefaultRepositoryClassName();

        // Constructor injection, I check with subclass of but it is just an example
        if(is_subclass_of($repositoryClassName, ServiceLocatorAwareInterface::class)){
            $serviceLocator = $this->getServiceLocator()
            $repository = new $repositoryClassName($entityManager, $metadata, $serviceLocator);
        }else{
            $repository = new $repositoryClassName($entityManager, $metadata);
        }
        return $repository;
    }
}
Run Code Online (Sandbox Code Playgroud)

2)为存储库工厂创建工厂

<?php
namespace My\ORM\Repository\Factory;

use My\ORM\Repository\CustomRepositoryFactory;
use Zend\Cache\Storage\StorageInterface;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class CustomRepositoryFactoryFactory implements FactoryInterface
{
    /**
     * @param  ServiceLocatorInterface $serviceLocator
     * @return StorageInterface
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        return new CustomRepositoryFactory($serviceLocator);
    }
}
Run Code Online (Sandbox Code Playgroud)

3)在service_manager配置中注册存储库工厂的工厂

'service_manager' => array(
    'factories' => array(
        'My\ORM\Repository\CustomRepositoryFactory' => 'My\ORM\Repository\Factory\CustomRepositoryFactoryFactory'
    )
)
Run Code Online (Sandbox Code Playgroud)

4)在doctrine配置中注册存储库工厂

'doctrine' => array(
    'configuration' => array(
        'orm_default' => array(
            'repository_factory' => 'My\ORM\Repository\CustomRepositoryFactory'
        )
    )
)
Run Code Online (Sandbox Code Playgroud)