在我的 Zend 应用程序中,我为每个表都有单独的模型,并使用以下方法检索数据TableGateway
现在我需要实现表单来创建编辑页面。我可以创建一个表格/模型的表格,如http://framework.zend.com/manual/2.2/en/user-guide/forms-and-actions.html中所述
这是我的编辑操作-
public function editAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('candidate', array(
'action' => 'index'
));
}
try {
$candidate = $this->getCandidateTable()->getCandidate($id);
}
catch (\Exception $ex) {
return $this->redirect()->toRoute('candidate', array(
'action' => 'index'
));
}
$form = new CandidateForm();
$form->bind($candidate);
$form->get('submit')->setAttribute('value', 'Edit');
$request = $this->getRequest();
if ($request->isPost()) {
$form->setInputFilter($candidate->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$this->getCandidateTable()->saveCandidate($candidate);
return $this->redirect()->toRoute('candidate');
}
}
return array(
'id' => $id,
'form' => $form,
);
}
编辑视图 -
<?php
$title = 'Edit Candidate';
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<?php
$form = $this->form;
$form->setAttribute('action', $this->url(
'candidate',
array(
'action' => 'edit',
'id' => $this->id,
)
));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formHidden($form->get('id'));
echo $this->formRow($form->get('title'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
此编辑操作将表单与一个表 (CandidateTable) 绑定。但在我的应用程序中,该页面包含来自多个表(CandidateSkills、CandidateQualifications 等)的数据。当我单击提交时,它应该将数据保存在单独的表中。