cakephp - 在 CakePHP 中保存数据表单

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

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

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

    ?>  

我的 Controller :

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

The Error 'UPDATE'

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

SQL 查询:SELECT IndicatorDatum . id , IndicatorDatumUpdate .*, (CONCAT( IndicatorDatum . indicator_id ,'_', IndicatorDatum . report_year_id )) AS IndicatorDatum__indexKey来自 astest . indicator_data作为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_id IN () GROUP BY s.state,s。 indicator_datum_id) AS LatestStateUpdate ON ( IndicatorDatum . id = LatestStateUpdate . indicator_datum_id AND IndicatorDatum . state = LatestStateUpdate . state ) 内部连接 ​​astest . indicator_datum_updates作为IndicatorDatumUpdate ON ( IndicatorDatumUpdate . indicator_datum_id = LatestStateUpdate . indicator_datum_id AND IndicatorDatumUpdate . created = LatestStateUpdate . update_date ) WHERE IndicatorDatum . id =(空)

最佳答案

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

WHERE s.indicator_datum_id IN ()

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

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

关于cakephp - 在 CakePHP 中保存数据表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30753353/

相关文章:

php - 为什么 CakePHP 将 _method 设置为 PUT 而不是 POST

c - 如何将内存中的音频数据保存为wav文件?

CakePHP 2.1.1。 testAction() 未定义

c# - 使用 Google Play 游戏保存多个变量

sql - 在 Cakephp 中使用不同的查询查找所有内容

cakephp - 什么是 CakePHP 行为?

css - 除了 css 之外的 html 类的使用

php - CakePHP 将模型名称添加到查询返回的信息中

php - 未找到 PostsController::index() - CakePHP

arrays - 在 Perl 中存储和读取文件中的哈希值和数组