ZF2 Form Hydration with multiple objects and fieldsets

蹲街弑〆低调 提交于 2019-12-12 15:33:11

问题


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

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

My team fieldset:

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.
    }
}

My team form:

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',
            ),
        ));
    }
}

And in my controller:

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.

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.

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?

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


回答1:


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

The documentation states

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.



来源:https://stackoverflow.com/questions/23659946/zf2-form-hydration-with-multiple-objects-and-fieldsets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!