我在数据库中有两列,id和表名下的列。 我的编辑功能:
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-&gt; Session-&gt; setFlash(“数据无法编辑”);被执行。
答案 0 :(得分:0)
Try: $this->Category->id = $this->request->data['id']; 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');
}
答案 1 :(得分:0)
将$id
传递给视图 -
$this->set(compact('id'));
然后将其设置为字段 -
echo $this->Form->input('id',array('type'=>'hidden', 'value' => $id));
如果您有隐藏的ID字段,那么为什么要显示另一个?如果不需要删除,否则正确分配值。希望它能奏效。