在CakePHP中保存数据表单

时间:2015-06-10 10:11:50

标签: cakephp save cakephp-2.0 has-and-belongs-to-many

我遇到了保存数据的问题“我的表单没有传递正确的数组”,我想知道我的代码中的错误是什么(阅读完食谱后)我理解传递给保存方法的数组应该是像这样

array(
    (int) 0 => array(
        'value' => '',
        'id' => '',
        'indicator_id' => '283',
        'report_year_id' => '7',
        'Assurance' => array(
            (int) 0 => '1',
            (int) 1 => '2',
            (int) 2 => '3',
            (int) 3 => '4',
            (int) 4 => '5'
        )
    ),
    (int) 1 => array(
        'value' => '',
        'id' => '',
        'indicator_id' => '283',
        'report_year_id' => '6',
        'Assurance' => ''))

但是当我调试代码时,我发现数据传递给了save方法:

array(
    'currentOrg' => array(
        'id' => '40'
    ),
    'IndicatorDatum' => array(
        '$cn' => array(
            'id' => '',
            'comment' => '',
            'reference' => ''
        ),
        (int) 0 => array(
            'value' => '',
            'Assurance' => ''
        ),
        (int) 1 => array(
            'value' => '',
            'Assurance' => ''
        ),
        (int) 2 => array(
            'value' => '',
            'Assurance' => ''
        ),
        (int) 3 => array(
            'value' => '',
            'Assurance' => ''
        ),

我的表单:

echo $this->DashboardForm->create('IndicatorDatum',array(
    'url' => array('controller'=>'indicator_data','action'=>'edit_group', 'dashboard'=>true, $thisGroup['IndicatorGroup']['id']),
    'novalidate'=>true
    ));
            <?  $cn = 0; 
            foreach ($years as $year) :
            echo $this->Form->hidden("IndicatorDatum.$cn.id");
            echo $this->Form->hidden("IndicatorDatum.$cn.state");
            echo $this->Form->input("IndicatorDatum.$cn.indicator_id",array(
                                                'type'=>'hidden', 'default'=>$iid
                                            )); 
        echo $this->Form->input("IndicatorDatum.$cnt.report_year_id",array(
                                            'type'=>'hidden', 'default'=>$yid
                                        )); 
            echo $this->Form->input('IndicatorDatum.$cn.value');                                   
            echo $this->Form->input("IndicatorDatum.$cn.Assurance", array('style'=>'width: 165px;',
                                                        'type'=>'select',
                                                        'multiple'=>true, 'options' => $assurances, 'selected' => $selected,'label' => false));
            $cn++;
            endforeach;

     echo $this->Form->submit(__('Save All'));

    ?>  

我的控制器:

if ($this->request->is('post')|| $this->request->is('put')) {
            $data = debug($this->request->data);
                        if ($this->IndicatorDatum->saveAll($this->request->data)) {
                $this->Session->setFlash(__('The indicator data has been saved'), 'flash/success');
                $this->redirect(array('action'=>'edit_group',$group_id));
            } else {
                $this->Session->setFlash(__('The indicator data could not be saved. Please verify the fields highlighted in red and try again.'), 'flash/error');
            }
        }
  

错误'更新'

我去DATABASE ERROR 错误:SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;查看与您的MySQL服务器版本对应的手册,以便在')GROUP BY s.state,s.indicator_datum_id附近使用正确的语法。在第4行AS LatestStateUpdate ON'

SQL查询:SELECT IndicatorDatumidIndicatorDatumUpdate。*,(CONCAT(IndicatorDatumindicator_id,'_',{{1} }。IndicatorDatum))AS report_year_id FROM IndicatorDatum__indexKeyastest AS indicator_data INNER JOIN(选择u.indicator_datum_id,MAX(u.created)为update_date, s.state FROM indicator_datum_states s INNER JOIN indicator_datum_updates u on u.id = s.indicator_datum_update_id WHERE s.indicator_datum_id IN()GROUP BY s.state,s.indicator_datum_id)AS IndicatorDatum ON(LatestStateUpdate。{ {1}} = IndicatorDatumidLatestStateUpdateindicator_datum_id = IndicatorDatumstate)INNER JOIN LatestStateUpdate。{ {1}} AS state ON(astestindicator_datum_updates = IndicatorDatumUpdateIndicatorDatumUpdateindicator_datum_idLatestStateUpdate = {{ 1}}。indicator_datum_id)WHERE IndicatorDatumUpdatecreated =(NULL)

1 个答案:

答案 0 :(得分:1)

由于您的空IN()而导致错误,并且似乎与saveAll()无关:

WHERE s.indicator_datum_id IN ()

这个SELECT在哪里建造?我建议在构建该条件之前先检查它是否为空。

(很难说它来自哪里 - 它看起来不像来自任何提供的代码。)