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