Doctrine orm Zend Framework 2中正在使用的实体管理器
我已经为我的模块编写了一个定制服务。此服务提供公共静态函数,这些函数应验证给定令牌 现在我想实现另一个公共静态函数,它检查条令实体是否存在。对于这种情况,我的服务中需要对象管理器或服务定位器Doctrine orm Zend Framework 2中正在使用的实体管理器,doctrine-orm,zend-framework2,Doctrine Orm,Zend Framework2,我已经为我的模块编写了一个定制服务。此服务提供公共静态函数,这些函数应验证给定令牌 现在我想实现另一个公共静态函数,它检查条令实体是否存在。对于这种情况,我的服务中需要对象管理器或服务定位器 class服务 { 常数键长度=10; const USE_number=true; const USE_CHARS=true; 公共静态函数有效($apiKey){ $isValid=false; #更多代码待定 $isValid=self::exists($apiKey); 返回$isValid; }
class服务
{
常数键长度=10;
const USE_number=true;
const USE_CHARS=true;
公共静态函数有效($apiKey){
$isValid=false;
#更多代码待定
$isValid=self::exists($apiKey);
返回$isValid;
}
存在公共静态函数($apiKey){
#在此处插入对象管理器
$validator=new\DoctrineModule\validator\ObjectExists(数组)(
'object\u repository'=>$objectManager->getRepository('Application\Entity\User'),
'字段'=>数组('电子邮件')
));
}
}
doesEntityExist()
函数的最佳实践是什么最好的方法是从类中完全删除静态方法。ZF2使按名称获取服务变得非常容易,因此对于这种用例,您不应该真正需要静态方法 首先,清理您的服务:
名称空间MyApp\Service;
使用条令\Common\Persistence\ObjectRepository;
使用doctriemodule\Validator\ObjectExists;
班级服务
{
// ...
受保护的$validator;
公共函数构造(ObjectRepository$ObjectRepository)
{
$this->validator=new\DoctrineModule\validator\ObjectExists(数组)(
“对象存储库”=>$objectRepository,
'字段'=>数组('电子邮件')
));
}
存在公共函数($apiKey)
{
返回$this->validator->isValid($apiKey);
}
// ...
}
现在为它定义一个工厂:
名称空间MyApp\ServiceFactory;
使用MyApp\Service\ApiService;
使用Zend\ServiceManager\FactoryInterface;
使用Zend\ServiceManager\ServiceLocator接口;
类ApiServiceFactory实现FactoryInterface
{
公共函数createService(serviceLocator接口$serviceLocator)
{
$entityManager=$serviceLocator->get('Doctrine\ORM\entityManager');
$repository=$entityManager->getRepository('Application\Entity\User');
返回新的ApiService($repository);
}
}
然后将服务名称映射到工厂(通常在模块中):
名称空间MyApp;
使用Zend\ModuleManager\Feature\ConfigProviderInterface;
类模块实现ConfigProviderInterface
{
公共函数getConfig()
{
返回数组(
“服务管理器”=>阵列(
“工厂”=>数组(
“MyApp\Service\ApiService”
=>“MyApp\ServiceFactory\ApiServiceFactory”,
),
),
);
}
}
注意:您可能希望简单地使用闭包,而不是定义单独的工厂类,但是在不使用服务时,使用工厂类可以稍微提高性能。此外,在配置中使用闭包意味着不能缓存合并的配置,所以请考虑使用此处建议的方法。
这是一个没有工厂类的例子(再次考虑使用上面解释的方法):
名称空间MyApp;
使用Zend\ModuleManager\Feature\ServiceProviderInterface;
类模块实现ServiceProviderInterface
{
公共函数getServiceConfig()
{
返回数组(
“工厂”=>数组(
'MyApp\Service\ApiService'=>函数($sl){
$entityManager=$serviceLocator->get('Doctrine\ORM\entityManager');
$repository=$entityManager->getRepository('Application\Entity\User');
返回新的MyApp\Service\ApiService($repository);
},
),
);
}
}
现在,您可以在控制器中使用该服务:
MyController类扩展了AbstractActionController
{
// ...
公共职能行动()
{
$apiService=$this->getServiceLocator()->get('MyApp\Service\apiService');
如果(!$apiService->isValid($this->params('api-key')){
抛出新的InvalidApiKeyException($this->params('api-key'));
}
// ...
}
// ...
}
您还可以在任何具有service manager的位置检索它:
$validator=$serviceLocator->get('MyApp\Service\ApiService');
作为一个附加的建议,考虑简化您的服务。因为
名称空间MyApp;
使用Zend\ModuleManager\Feature\ServiceProviderInterface;
使用doctriemodule\Validator\ObjectExists;
类模块实现ServiceProviderInterface
{
公共函数getServiceConfig()
{
返回数组(
“工厂”=>数组(
'MyApp\Validator\ApiKeyValidator'=>函数($sl){
$entityManager=$serviceLocator->get('Doctrine\ORM\entityManager');
$repository=$entityManager->getRepository('Application\Entity\User');
存在新的ObjectExists(数组(
“对象存储库”=>$objectRepository,
'字段'=>数组('电子邮件')
));
},
),
);
}
}
个人,我会让服务成为“服务”,把它放在Service MeaMaGER中。另外,我会考虑重构代码。现在,您对ObjistValueValueAtter有一个依赖性,它依赖于实体存储库,而DE。
class ApiService
{
protected $validator;
public function isValid($apiKey)
{
// other code
$isValid = $this->exists($apiKey);
}
public function exists($apiKey)
{
return $this->getValidator()->isValid($apiKey);
}
public function setValidator(\Zend\Validator\AbstractValidator $validator)
{
$this->validator = $validator;
return $this;
}
public function getValidator()
{
return $this->validator;
}
}
public function getServiceConfig()
{
return array(
'factories' => array(
'ApiService' => function($sm) {
$em = $sm->get('Doctrine\ORM\EntityManager');
$repo = $em->getRepository('Application\Entity\User');
$validator = new \DoctrineModule\Validator\ObjectExists($repo,
array('fields' => array('email')));
$service = new ApiService();
$service->setValidator($validator);
return $service;
},
),
);
}