【发布时间】:2014-12-27 16:19:24
【问题描述】:
我正在关注第 1 章第 19 页中的 cakephp 2 应用程序说明书 或在这里: http://www.cakedc.com/cakephp-tutorials/adding-and-editing-records
我检查了我的代码几次,但找不到阻止我更新记录的错误
如果我烘焙产品数据库,我可以完成所有的 crud 操作 我的添加工作正常,所以表单元素应该工作正常
在我的 ProductsController 编辑函数中,由于某种原因它没有发布
if($this->Product->save($this->request->data)){
$this->Session->setFlash(__('Product updated'));
return $this->redirect(array('action' => 'index'));
}
ProductsController.php 在 /cakephp/app/Controller
<?php
App::uses('AppController', 'Controller');
class ProductsController extends AppController {
public $helpers = array('Html', 'Form');
public $components = array('Session', 'Paginator');
Public $paginate = array('limit' => 10);
public function index() {
$this->Product->recursive = -1;
$this->set('products', $this->paginate());
}
public function view($id) {
if (!($product = $this->Product->findById($id))) {
throw new NotFoundException(__('Product not found'));
}
$this->set(compact ('product'));
}
public function add() {
if($this->request->is('post')){
$this->Product->create();
if($this->Product->save($this->request->data)){
$this->Session->setFlash(__('New product created'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Could not create product'));
}
}
public function edit($id){
$product = $this->Product->findById($id);
if(!$product){
throw new NotFoundException(__('Product not found'));
}
if ($this->request->is('post')){
$this->Product->id = $id;
if($this->Product->save($this->request->data)){
$this->Session->setFlash(__('Product updated'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Product could not be updated'));
}else{
$this->request->data = $product;
}
}
}
edit.ctp 文件
<?php echo $this->element('Products/form'); ?>
他们在书中声明 add.ctp 和 edit.ctp 之间的标题应该看起来不同,但对于我的生活,我看不出有什么区别。
在 form.ctp 中也缺少
?>
想知道这是不是笔误...
【问题讨论】:
-
在 form.ctp 中你是否使用过 echo $this->Form->input('id'); ?
-
ctp 文件中缺少的 ?> 肯定是错字。
-
.input('id');修好了。