具有多个对象和字段集的ZF2表单水化 [英] ZF2 Form Hydration with multiple objects and fieldsets

查看:85
本文介绍了具有多个对象和字段集的ZF2表单水化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力寻找如何用一个表格编辑多个模型的方法。

I'm struggling trying to work out how to edit multiple models with a single form.

我有一个称为Teams的数据库表和一个与此表相关联的学说实体。我创建如下表单:

I have a database table called Teams and a doctrine entity associated with this table. I create a form as below:

我的团队字段集:

class TeamFieldset extends AbstractFieldset implements InputFilterProviderInterface
{
    public function init()
    {
        $this->setName('Team')
            ->setHydrator(new DoctrineHydrator($this->getObjectManager(),'Application\Model\Entities\Team'))
            ->setObject(new Team())
            ->setLabel('Team');

        $this->add(array(
            'type' => 'Hidden',
            'name' => 'id',
        ));

        $this->add(array(
            'name' => 'name',
            'options' => array(
                'label' => 'Team name',
            ),
        ));

        // ….  more fields go here
    }


    /**
     * Implement InputFilterProviderInterface
     */
    public function getInputFilterSpecification()
    {
        // …. input filter implementation goes here.
    }
}

我的团队形式:

class TeamForm extends AbstractAdminForm
{
    public function init()
    {
        parent::init();

        $this->setName('team-form')
            ->add(array(
                'type' => 'TeamFieldset',
                'name' => 'Team',
                'options' => array(
                     'use_as_base_fieldset' => true,
                ),
            )
        );

        $this->add(array(
            'name' => 'submit',
            'options' => array(
                'label' => 'Save Team',
            ),
            'attributes' => array(
                'class' => 'btn-primary',
                'type' => 'submit',
            ),
        ));
    }
}

在我的控制器中:

public function editTeamAction()
{
    $team = $this->getEntityManager()->find('Application\Model\Entities\Team',$this->params()->fromRoute('team_id'));

    $formManager = $this->serviceLocator->get('FormElementManager');
    $form = $formManager->get('Application\Form\Team\TeamForm');
    $form->setAttribute('action',$_SERVER['REQUEST_URI']);
    $form->bind($team);

    $request = $this->getRequest();
    if ($request->isPost()) {
        $form->setData($request->getPost());
        if ($form->isValid()) {
            $this->getEntityManager()->persist($team);
            $this->getEntityManager()->flush();
            $this->redirect()->toRoute('admin/leagues/league/team',array('league_id' => $team->getLeague()->getId(),'team_id' => $team->getId()));
        }
    }

    return array(
        'team' => $team,
        'form' => $form
    );
}

到目前为止,一切都很好,效果很好。

So far this is all fine and works great.

现在,我还有一个旧数据库,其中有另一个Teams表。我希望用户可以通过同一表格进行编辑。

Now, I also have a legacy database with another Teams table in it. I'd like the user to be able to edit both via the same form.

我对旧式数据库不使用原则,但这无关紧要,我可以很快将适当的记录提取到数组中,然后使用数组创建一个字段集

I don't use doctrine for the legacy database, but this is irrelevant and I can soon pull the appropriate record out into an array and then create a fieldset with an array hydrator for it.

但是,您在窗体上而不是在字段集上调用bind函数。那么,如何使用表单上的此单个绑定操作将数据绑定到每个字段集?

However, you call the bind function on a form, not on a fieldset. So how do I bind data to each fieldset with this single bind operation on the form?

如果在fielset上进行绑定操作就不会有问题,我可以从表单中拉出每个字段集并与适当的对象绑定。

If there were a bind operation on a fielset it wouldn't be an issue, I could pull each fieldset from the form and bind with appropriate object.

任何指针都将不胜感激。

Any pointers would be very much appreciated.

:wq

推荐答案

您可以使用 Zend\Stdlib\Hydrator\Aggregate\AggregateHydrator

You can use the Zend\Stdlib\Hydrator\Aggregate\AggregateHydrator

文档声明


当您要水合或提取水时,通常希望使用集水器来自实现多个接口的复杂对象的数据,因此需要多个水合器在后续处理中步骤。

You typically want to use an aggregate hydrator when you want to hydrate or extract data from complex objects that implement multiple interfaces, and therefore need multiple hydrators to handle that in subsequent steps.

这篇关于具有多个对象和字段集的ZF2表单水化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆