【问题标题】:Saving Data form in CakePHP在 CakePHP 中保存数据表单
【发布时间】:2015-08-25 12:41:48
【问题描述】:

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

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' => ''))

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

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

错误“更新”

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

SQL 查询:SELECT IndicatorDatum.id, IndicatorDatumUpdate.*, (CONCAT(IndicatorDatum.indicator_id,'_',IndicatorDatum.report_year_id)) AS IndicatorDatum__indexKey FROM astest.indicator_data AS IndicatorDatum INNER JOIN (SELECT u.indicator_datum_id, MAX(u.created) as 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 IN () GROUP BY s.state, s.indicator_datum_id) AS LatestStateUpdate ON (IndicatorDatum.id = LatestStateUpdate.indicator_datum_id AND IndicatorDatum.state = LatestStateUpdate.@986 ) INNER JOIN astest.indicator_datum_updates AS IndicatorDatumUpdate ON (IndicatorDatumUpdate.indicator_datum_id = LatestStateUpdate.indicator_datum_id AND IndicatorDatumUpdate.created=@98765435655556WHER@98765435655. 987654357@.id = (NULL)

【问题讨论】:

  • 您好,数据没有保存在数据库中吗?检查输入字段名称和表字段是否相同?
  • 是的,是一样的,我想知道为什么我传错了数组?
  • 告诉我?你有什么错误?
  • 这是我尝试保存数据时得到的结果(无法保存指标数据。请验证以红色突出显示的字段,然后重试。)
  • 你更新了什么问题?

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


【解决方案1】:

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

WHERE s.indicator_datum_id IN ()

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-18
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2012-05-24
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    相关资源
    最近更新 更多