如何将对象添加到InputFilter,它是否有某种工厂?

时间:2013-10-24 16:10:21

标签: doctrine-orm zend-framework2

我的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但由于它没有通过服务管理器,因此未设置。

1 个答案:

答案 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',
    ),
),