在数据库中存储值的问题(cakePHP)

时间:2009-06-23 06:04:35

标签: mysql database cakephp

我有一个名为required的复选框,如果单击该复选框,则其值为1,否则为0。 该值从视图文件正确传递到控制器。模型文件从控制器获取值,但未保存该值。

我在模型中回显了所需的值,以检查是否收到了值。该值回显为1.但在数据库中,它不会更新。

这是我的代码:

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); 

}

修改

我还检查了是否使用saveField选项保存了所需的值。

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

值1存储在表中该特定属性的单独行中。那么问题是什么,为什么它不与其他值一起保存。

修改

如果我直接保存整数值,比如

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

它存储在数据库中。怎么样??那么问题是什么?

1 个答案:

答案 0 :(得分:1)

我从一些重构开始:

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); 
}

那么我会尝试Travis Leleu的实际调试代码的建议。我首先检查保存前$this->Attribute->id的值。