I have two columns in database, id and name under the table categories. My edit function:
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');
}
}
My categories/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')); ?>
Whenever I try to edit and update the form, $this->Session->setFlash("The data could not be edited"); gets executed.