如果您没有任何有效/强有力的理由来实例化自定义实体存储库,您应该更喜欢扩展Doctrine\ORM\EntityRepository
在你的存储库中,例如CommissionRepository
。例如;
use Doctrine\ORM\EntityRepository;
class CommissionRepository extends EntityRepository
{
// No need to think about $em here. It will be automatically
// injected by doctrine when you call getRepository().
//
function fetchCommissionById($id)
{
// You can easily get the object manager directly (_em) or
// using getEntityManager() accessor method in a repository
return $this->_em->find($id);
}
}
通过这种方式,当您调用时,实体管理器将在构建时自动注入到存储库中$em->getRepository('App\Entity\Commission')
method.
我假设你已经有一个Commission
您的应用程序中的实体Entity
命名空间:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repo\CommissionRepository")
* @ORM\Table
*/
class Commission
{
}
然后您可以简化工厂中存储库的注入过程,例如:
// ZF2 Way
class CommissionControllerFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $services)
{
$em = $services->getServiceLocator()->get('doctrine');
$repository = $em->getRepository('App\Entity\Commission');
return new CommissionController($repository);
}
}
UPDATE- 随着 Service Manager V3 的发布,FactoryInterface 已移至Zend\ServiceManager\Factory
命名空间 (1),工厂实际上是可调用的 (2),并且可以与任何容器互操作 https://github.com/container-interop/container-interop兼容 DIC (3) 更新后的工厂如下所示:
// ZF3 Way
use Zend\ServiceManager\Factory\FactoryInterface;
use Interop\Container\ContainerInterface;
use Doctrine\ORM\EntityManager;
class CommissionControllerFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $dic, $name, array $options = null) {
$em = $dic->get(EntityManager::class);
$repository = $em->getRepository('App\Entity\Commission');
return new CommissionController($repository);
}
}
对于这个问题;正如马科什所说,懒惰服务 https://zendframework.github.io/zend-servicemanager/lazy-services/是在需要时立即创建服务的方法。 ZF3 发布后将使用 zend-servicemanager 3.0 组件。 (目前 zend-expressive 使用它)截至服务管理器 v3 https://github.com/zendframework/zend-servicemanager/releases?after=release-2.7.2您可以通过定义创建一些代理服务惰性服务 https://github.com/zendframework/zend-servicemanager/blob/f39c95385712ea5acb264aa3299c306423d768ed/src/ConfigInterface.php#L32-L40 and 代表者 https://zendframework.github.io/zend-servicemanager/delegators/在您的服务配置中:
'factories' => [],
'invokables' => [],
'delegators' => [
FooService::class => [
FooServiceDelegatorFactory::class,
],
],
'lazy_services' => [
// map of service names and their relative class names - this
// is required since the service manager cannot know the
// class name of defined services up front
'class_map' => [
// 'foo' => 'MyApplication\Foo',
],
// directory where proxy classes will be written - default to system_get_tmp_dir()
'proxies_target_dir' => null,
// namespace of the generated proxies, default to "ProxyManagerGeneratedProxy"
'proxies_namespace' => null,
// whether the generated proxy classes should be written to disk or generated on-the-fly
'write_proxy_files' => false,
];
另外,从服务管理器 v3 开始工厂 https://github.com/zendframework/zend-servicemanager/blob/master/src/Factory/FactoryInterface.php#L39兼容容器接口 https://github.com/container-interop/container-interop/。为了向前兼容,您可能需要保留两者__invoke()
and createService()
工厂中的顺利迁移方法。
最后,你的ZF3兼容工厂可能看起来像:
class CommissionControllerFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $name, array $options = null)
{
$em = $container->get('doctrine');
$repository = $em->getRepository('App\Entity\Commission');
return new CommissionController($repository);
}
public function createService(ServiceLocatorInterface $container, $name = null, $requestedName = null)
{
return $this($container, $requestedName, []);
}
}
希望能帮助到你。