如何在保温策略类中获得服务定位器?

时间:2014-07-30 19:23:20

标签: php zend-framework2

我想在我的保湿策略课中获得服务定位器。这是我的战略课:

<?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访问服务定位器,但我无法使用它。谢谢你的帮助

1 个答案:

答案 0 :(得分:2)

zf2水化器中的

用于将数据集从php objects水合为array,反之亦然,以便将arrays中的数据填充到object中。

就是这样,没有理由在zf2中默认将servicemanager注入水化器。你尝试以某种方式处理这些数据,因为zf2中的普通水合物不是主要设计的。

您呼叫实体并getRepository,所以我认为您使用Doctrine。如果是这样,请查看DoctrineModule\Stdlib\Hydrator\DoctrineObject类,将表单数据绑定到一个主题实体中。