0

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??

6
  • Are any of the other values saved? Or is it just "required"? Commented Jun 23, 2009 at 6:15
  • All the other values are saved correctly,like name,size are all updated correctly. Only the required value is not being saved.I added this required property recently.Earlier I had only the name,size type ,instructions and sequence no Commented Jun 23, 2009 at 6:26
  • Wasn't this resolved with your previous question? stackoverflow.com/questions/1030639/… Commented Jun 23, 2009 at 8:03
  • That was a different function where i wanted to save a default value. But in this function,I get the value of the check box(0 if unchecked,1 if checked) from the view and want to save this value to the database. I also cleared the cache folder,yet the value 1 is not saved if the check box is clicked. Commented Jun 23, 2009 at 8:19
  • Turn debug to level 2, identify the SQL code where it's being inserted into the database, and show that to us here. Also, in your model, in the beforeSave( $data ) method, try a var_dump( $data ) and see if your "required" value is correctly passed to the model. Commented Jun 23, 2009 at 15:47

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.