【发布时间】:2015-04-13 04:55:47
【问题描述】:
我在数据库中有两列,表类别下的 id 和 name。 我的编辑功能:
public function edit($id = null){
if($this->Category->exists($id)){
throw new NotFoundException (__('Id was not found '));
}
if($this->Auth->user('role')=='admin'){
if(!$id){
throw new NotFoundException(__('Id was not set'));
}
$data=$this->Category->findById($id);
if(!$data){
throw new NotFoundException(__('Id was not found in database'));
}
if($this->request->is(array('post','put')) ){
if($this->Category->save($this->request->data)){
$this->Session->setFlash('The data has been edited successfully');
return $this->redirect(array('action' => 'index'));
//$this->redirect('index');
}
else{
$this->Session->setFlash("The data could not be edited");
$this->redirect(array('controller'=>'categories','action'=>'index'));
}
}
// else{
// $options = array('conditions' => array('Category.' .
$this->Category->primaryKey => $id));
// $this->request->data = $this->Category->find('first', $options);
// }
$this->request->data=$data;
}
else{
$this->Session->setFlash(__('You do not have right to do this'));
$this->redirect('index');
}
}
我的类别/edit.ctp
<?php echo $this->Form->create('Category'); ?>
<fieldset>
<legend><?php echo __('Edit Category'); ?></legend>
<?php
echo $this->Form->input('id');
echo $this->Form->input('name');
echo $this->Form->input('id',array('type'=>'hidden'));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
每当我尝试编辑和更新表单时, $this->Session->setFlash("无法编辑数据");被执行。
【问题讨论】:
-
您是否在编辑中打印了请求值?使用 pr($this->request->data) 并检查。同时启用你的 debig 模式,看看是否有任何错误显示
-
需要更多优化,一旦问题解决,我会通知您
-
@iamaphpdeveloper。它正在打印编辑值,并且 id 也是正确的。仍然没有进入 ($this->Category->save($this->request->data))。
标签: cakephp