我的InputFilter:
namespace FarmAccounts\InputFilter;
use Zend\InputFilter\InputFilter;
class Account extends InputFilter
{
public function init()
{
$factory = $this->getFactory();
$owner = $factory->createInput(array(
'name' => 'owner',
'required' => true,
'validators' => array(
array(
'name' => 'DoctrineModule\Validator\NoObjectExists',
'options' => array(
'object_repository' => $entityManager->getRepository('OAuth2Server\Entity\User'),
'fields' => 'id'
)
)
)
));
$this->add($owner);
}
}
然后当我需要这个过滤器时,我打电话:
$filter = $this->serviceManager->get('InputFilterManager')
->get('FarmAccounts\InputFilter\Account');
正如您所看到的,$entityManager
未在任何地方定义,这就是我的问题,我不知道如何在此处添加它。 InputFilter没有服务定位器。也许有办法为inputfitler创建工厂?
更新:
我已尝试实施ServiceLocatorAwareInterface
但由于它没有通过服务管理器,因此未设置。
答案 0 :(得分:0)
看起来我管理解决了我的问题
工厂:
namespace FarmAccounts\Factory;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use FarmAccounts\InputFilter\Account as AccountInputFilter;
class AccountInputFilterFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$input_filter = new AccountInputFilter;
$entityManager = $serviceLocator->getServiceLocator()->get('Doctrine\ORM\EntityManager');
$input_filter->setEntityManager($entityManager);
return $input_filter;
}
}
和module.config.php:
'input_filters' => array(
'factories' => array(
'FarmAccounts\InputFilter\Account' => 'FarmAccounts\Factory\AccountInputFilterFactory',
),
),