如何通过ZF2 FormElementManager将Doctrine ObjectManager传递给表单

时间:2015-12-31 22:04:04

标签: forms doctrine-orm zend-framework2

我想在ZF2中创建自定义表单元素,这需要FormElementManager。我目前在表单创建中使用Doctrine Hydrator,如this tutorial所示。在此方法中,在控制器中创建ObjectManager对象,并在实例化时将其传递给新表单:

public function editAction()
{
    // Get your ObjectManager from the ServiceManager
    $objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');

    // Create the form and inject the ObjectManager
    $form = new UpdateBlogPostForm($objectManager);

    // …

更新表格:

namespace Application\Form;

use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Form;

class UpdateBlogPostForm extends Form
{
    public function __construct(ObjectManager $objectManager)
    {
        parent::__construct('update-blog-post-form');

        // The form will hydrate an object of type "BlogPost"
        $this->setHydrator(new DoctrineHydrator($objectManager));

        // Add the user fieldset, and set it as the base fieldset
        $blogPostFieldset = new BlogPostFieldset($objectManager);
        $blogPostFieldset->setUseAsBaseFieldset(true);
        $this->add($blogPostFieldset);

        // … add CSRF and submit elements …

        // Optionally set your validation group here
    }
}

BlogPost字段集:

namespace Application\Form;

use Application\Entity\BlogPost;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;

class BlogPostFieldset extends Fieldset implements InputFilterProviderInterface
{
    public function __construct(ObjectManager $objectManager)
    {
        parent::__construct('blog-post');

        $this->setHydrator(new DoctrineHydrator($objectManager))
             ->setObject(new BlogPost());

        // … add fieldset elements …

    }

    // … public function getInputFilterSpecification() …

}

不幸的是,为了使用ZF2的FormElementManager,ZF2 manual说,“第二个问题是你不能直接实例化你的表单类,而是通过它来获取它的一个实例Zend \ Form \ FormElementManager;“所以我必须得到这样的表格:

 public function editAction()
 {
     $sl = $this->getServiceLocator();
     $form = $sl->get('FormElementManager')->get('\Application\Form\UpdateBlogPostForm');

有没有办法通过$objectManager将ObjectManager对象FormElementManager传递给表单,还是有办法在表单中创建对象,以便水印器和字段集可以使用它? / p>

1 个答案:

答案 0 :(得分:4)

DoctrineEntityHydratorFactory

创建水化器并注入对象管理器的工厂。

namespace MyModule\Stdlib\Hydrator;

use DoctrineORMModule\Stdlib\Hydrator\DoctrineEntity;

class DoctrineEntityHydratorFactory
{
    public function __invoke($hydratorPluginManager, $name, $requestedName)
    {
        $serviceManager = $hydratorPluginManager->getServiceLocator();
        $objectManager  = $serviceManager->get('Doctrine\ORM\EntityManager');

        return new DoctrineEntity($objectManager);
    }
}

BlogPostFieldset

可重复使用的字段集,其中包含博客帖子的所有元素,并使用EntityHydrator来保护BlogPost实体。

namespace MyModule\Form;

use Zend\Form\Fieldset;

class BlogPostFieldsetFactory
{
    public function __invoke($formElementManager, $name, $requestedName)
    {
        $serviceManager = $formElementManager->getServiceLocator();
        $hydrator = $serviceManager->get('HydratorManager')->get('DoctrineEntityHydrator');

        $fieldset = new Fieldset('blog_post');
        $fieldset->setHydrator($hydrator);
        $fieldset->setObject(new BlogPost);

        //... add fieldset elements.
        $fieldset->add(['...']);
        //...

        return $fieldset;
    }
}

UpdateBlogPostFormFactory

创建博客更新表单的工厂。这会附加字段集并设置the use_as_base_fieldset option which allows it to use the fieldsets hydrator

namespace MyModule\Form;

class UpdateBlogPostFormFactory
{
    public function __invoke($formElementManager, $name, $requestedName)
    {
        $form = new Form('update_blog_post);

        //...
        $factory = new \Zend\Form\Factory($formElementManager);
        $form->setFormFactory($factory);
        $form->add([
            'name' => 'blog_post',
            'type' => 'BlogPostFieldset',
            'options' => [
                'use_as_base_fieldset' => true,   
            ]
        ]);
        //...

        return $form;
    }
}

您还需要在module.config.php中向所需的经理注册服务。

return [
    'form_elements' => [
        'factories' => [
            'UpdateBlogPostForm' => 'MyModule\Form\UpdateBlogPostFormFactory',
            'BlogPostFieldset' => 'MyModule\Form\BlogPostFieldsetFactory',
        ],
    ],
    'hydrators' => [
        'factories' => [
            'DoctrineEntityHydrator' => 'MyModule\Stdlib\Hydrator\DoctrineEntityHydratorFactory',
        ],
    ],
];

因此,您可以在控制器中索取表格。

$form = $formElementManager->get('UpdateBlogPostForm');