我想在我的hydrator策略类中获得服务定位器。这是我的策略类:
<?php
namespace Application'Model'Strategies;
use Application'Model'Tables'Entity;
use Zend'Stdlib'Hydrator'Strategy'StrategyInterface;
use Zend'ServiceManager'ServiceLocatorAwareInterface;
use Zend'ServiceManager'ServiceLocatorInterface;
class carBrandStrategy implements StrategyInterface, ServiceLocatorAwareInterface {
protected $services;
public function extract($value) {
return "hi";
}
public function hydrate($value) {
$entity = new Entity($this->getServiceLocator);
$tableCarBrands = $entity->getRepository("CarBrands");
return $tableCarBrands->find($value);
}
public function setServiceLocator(ServiceLocatorInterface $serviceLocator) {
$this->services = $serviceLocator;
}
public function getServiceLocator() {
return $this->services;
}
}
我发现我可以使用ServiceLocatorInterface访问服务定位器,但我不能使用它。感谢您的帮助
zf2水合器中的
用于水合从php objects
到array
的数据集,反之则用于将arrays
的数据填充到object
。
就是这样,在zf2中没有理由默认将servicemanager注入到hydrator中。你尝试以一种zf2中普通水合器不是主要设计的方式来处理这些数据。
你调用一个实体和getRepository
,所以我认为你使用Doctrine
。如果是这样,请查看DoctrineModule'Stdlib'Hydrator'DoctrineObject
类,将表单数据绑定到原则实体中。