0
votes

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.

2
have you printed request values in edit? use pr($this->request->data) and check. Also enable your debig mode and see is there any error displaying - Butterfly
Few more optimization required, once issue fixed I will let you know - Butterfly
@iamaphpdeveloper. It's printing edited value and id is also correct.Still doesnot go inside ($this->Category->save($this->request->data)). - Bipin Paudel

2 Answers

0
votes
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');
       }
0
votes

Pass the $id to view -

$this->set(compact('id'));

Then set it to the field -

echo $this->Form->input('id',array('type'=>'hidden', 'value' => $id));

If you have a hidden id field then why are displaying another? Remove if not needed else assign the value properly. Hope it will work.