【发布时间】:2013-05-28 09:30:31
【问题描述】:
我正在使用AbstractTableGateway 和HydratingResultset 进行数据库操作。 (使用 BjyProfiler)
当我使用添加操作发布表单数据时,它可以工作,但编辑操作不起作用。当我进行绑定时,它可以工作,但我被重定向到添加页面,因为提交表单会重置来自路由的参数。
这是我的 editAction() 代码(与 Album editAction() 相同)
$id = (int)$this->params()->fromRoute('id');
if (!$id) {
return $this->redirect()->toRoute('voyage', array('action'=>'add'));
}
$voyage = $this->getVoyageTable()->getVoyage($id);
$form = new VoyageForm($this->getTypeVoyageTable());
$form->bind($voyage);
$form->get('submit')->setAttribute('value', 'Edit');
$request = $this->getRequest();
if ($request->isPost()) {
$form->setData($request->getPost());
if ($form->isValid()) {
$this->getVoyageTable()->saveVoyage($voyage);
// Redirect to list of voyages
return $this->redirect()->toRoute('voyage');
}
}
return array(
'id' => $id,
'form' => $form,
);
}
还有我的桌子:
class VoyageTable extends AbstractTableGateway
{
protected $table ='voyages';
public function __construct(Adapter $adapter)
{
$this->adapter = $adapter;
$this->resultSetPrototype = new HydratingResultSet();
$this->resultSetPrototype->setObjectPrototype(new Voyage());
$this->initialize();
}
[...]
有人可以帮助我吗?我该如何解决这个问题?谢谢。
【问题讨论】:
-
您是否真的在视图中设置了表单操作属性?如果您的路线与教程相似,您应该在 edit.phtml 中有类似
<?php $form->setAttribute('action', $this->url('voyage', array('id' => $id, 'action' => 'edit'))); ?>的内容,我猜您缺少id参数。 -
是的,我像你一样设置它
<h1><?php echo $this->escapeHtml($title); ?></h1> <?php $form = $this->form; $form->prepare(); $form->setAttribute('action', $this->url('voyage', array('action' => 'edit'))); $form->setAttribute('method', 'post'); echo $this->form()->openTag($form);?>...我在提交之前会获取操作 ID,因为当我们单击编辑按钮时绑定功能起作用。但是当我提交表单时,我丢失了将我重定向到添加页面的 id。 -
您在
$form->setAttribute('action', $this->url('voyage', array('action' => 'edit')));这一行中缺少'id' => $id键/值对。请参阅我之前的评论。 -
是的,你是对的,它现在可以工作了 :) 非常感谢。
标签: zend-framework2 routes zend-form2