views:

396

answers:

1

I have a checkbox named required,where if the checkbox is clicked its value is 1,otherwise its 0. The value is correctly passed form the view file to the controller. The model file gets the value from the controller, but the value is not being saved.

I echoed the required value in the model,to check if the value is received. The value is echoed as 1. But in the database,it is not updated.

This is my code:

function updateFieldEntries($data)
{
 $this->data['Attribute']['id']=$this->Attribute->find('all', array(
            'fields' => array('Attribute.id'),
            'order' => 'Attribute.id DESC' ));

    $this->data['Attribute']['id']=$this->data['Attribute']['id'][0]['Attribute']['id'];

 $this->data['Attribute']['form_id'] = $this->find('all', array(
            'fields' => array('Form.id'),
            'order' => 'Form.id DESC'));
    $this->data['Attribute']['form_id']=$this->data['Attribute']['form_id'][0]['Form']['id'];

    $this->data['Attribute']['instructions']=$data['Attribute']['instructions'];

    $this->data['Attribute']['required']=$data['Attribute']['required'];
 echo " required model ".$this->data['Attribute']['required'];


    $this->data['Attribute']['sequence_no'] =$this->Attribute->find('all', array(
        'conditions' => array('Attribute.form_id' =>$this->data['Attribute']['form_id']),
            'fields' => array('Attribute.sequence_no'),
            'order' => 'Attribute.sequence_no DESC'));
 $this->data['Attribute']['sequence_no']=$this->data['Attribute']['sequence_no'][0]['Attribute']['sequence_no'];

 if($data['Attribute']['name']== ''){
  $this->data['Attribute']['label']=$this->Attribute->find('all', array(
        'conditions' => array('Attribute.id' =>$this->data['Attribute']['id']),
            'fields' => array('Attribute.label')                                              ));
  $this->data['Attribute']['label']=$this->data['Attribute']['label'][0]['Attribute']['label'];
 }
 else{
  $this->data['Attribute']['label']= $data['Attribute']['name'];
 }

 if($data['Attribute']['size']== ''){
  $this->data['Attribute']['size']=$this->Attribute->find('all', array(
        'conditions' => array('Attribute.id' =>$this->data['Attribute']['id']),
            'fields' => array('Attribute.size')                                              ));
  $this->data['Attribute']['size']=$this->data['Attribute']['size'][0]['Attribute']['size'];
 }
 else{
  $this->data['Attribute']['size']= $data['Attribute']['size'];
 }


   if($data['Attribute']['instructions']== ''){
           $this->data['Attribute']['instructions']=$this->Attribute->find('all', array(
        'conditions' => array('Attribute.id' =>$this->data['Attribute']['id']),
            'fields' => array('Attribute.instructions')                                              ));
    $this->data['Attribute']['instructions']=$this->data['Attribute']['instructions'][0]['Attribute']['instructions'];

            }

 $this->Attribute->save($this->data); 

}

EDIT

I also checked if the required value alone is being saved using saveField option.

$this->Attribute->saveField('required',$this->data['Attribute']['required']);

The value 1 got stored in a separate row in the table for that particular attribute. So what is the problem, why is it not getting saved along with the other values.

EDIT

If I save an integer value directly,like

       $this->data['Attribute']['required']='7';

it gets stored in the database. How?? What is the problem then??

+1  A: 

I'd start with some refactoring:

function updateFieldEntries($data)
{
    $tempAttr = $this->Attribute->find
     (
      'first',
      array
      (
       'order' => 'Attribute.id DESC',
       'fields' => array('Attribute.id')
      )
     );

    $this->data['Attribute']['id'] = $tempAttr['Attribute']['id'];

    $tempAttr = $this->find
     (
      'first',
      array
      (
       'order' => 'Form.id DESC',
       'fields' => array('Form.id')
      )
     );

    $this->data['Attribute']['form_id'] = $tempAttr['Form']['id'];

    $this->data['Attribute']['instructions'] = $data['Attribute']['instructions'];
    $this->data['Attribute']['required'] = $data['Attribute']['required'];

    $tempAttr = $this->Attribute->find
     (
      'first',
      array
      (
       'order' => 'Attribute.sequence_no DESC',
       'fields' => array('Attribute.sequence_no'),
       'conditions' => array('Attribute.form_id' => $this->data['Attribute']['form_id'])
      )
     );

    $this->data['Attribute']['sequence_no'] = $tempAttr['Attribute']['sequence_no'];

    $tempAttr = $this->Attribute->find
     (
      'first',
      array
      (
       'conditions' => array('Attribute.id' => $this->data['Attribute']['id']),
       'fields' => array('Attribute.label', 'Attribute.size', 'Attribute.instructions')
      )
     );

    if (empty($data['Attribute']['name']))
    {
     $this->data['Attribute']['label'] = $tempAttr['Attribute']['label'];
    }
    else
    {
     $this->data['Attribute']['label'] = $data['Attribute']['name'];
    }

    if (empty($data['Attribute']['size']))
    {
     $this->data['Attribute']['size'] = $tempAttr['Attribute']['size'];
    }
    else
    {
     $this->data['Attribute']['size'] = $data['Attribute']['size'];
    }

    if (empty($data['Attribute']['instructions']))
    {
     $this->data['Attribute']['instructions'] = $tempAttr['Attribute']['instructions'];
    }
    else
    {
     $this->data['Attribute']['instructions'] = $data['Attribute']['instructions'];
    }

    $this->Attribute->save($this->data); 
}

Then I'd try Travis Leleu's recommendation of actually debugging your code. I'd start by checking the value of $this->Attribute->id before saving.

dr Hannibal Lecter