通常在cakehp2上,我会取消设置表单数据,一切都很好。
有时我会使用重定向来清除它。但在当前页面中我无法做到这一点。
有人发现了这个问题或解决方案吗?
如果您在成功添加一条记录后仍停留在"添加"页面,例如为了更快地输入多条记录,则需要在保存后重置实体。例如,如果你正在输入Posts,你的控制器会看起来像:
$post = $this->Posts->newEntity();
if ($this->request->is('post')) {
$post = $this->Posts->patchEntity($post, $this->request->data);
if ($this->Posts->save($post)) {
$post = $this->Posts->newEntity(); // <- Reset the entity
}
}
$this->set(compact('post'));
(为简洁起见,省略了错误检查、闪烁消息等。)
另一种选择是简单地重定向到同一页面。我有一个问题,不是所有的都被删除了
$contact = new ContactForm();
if ($this->request->is('post')) {
if ($contact->execute($this->request->data)) {
$this->Flash->success(__('Submited.'));
$this->request->data(null);
$contact = new ContactForm();
return $this->redirect('/(Same Page)');// This did the final trick for me
}
}