数据库错误 Cakephp

Posted

技术标签:

【中文标题】数据库错误 Cakephp【英文标题】:DataBase Error Cakephp 【发布时间】:2015-06-10 11:45:15 【问题描述】:

保存数据时出现数据库错误

错误

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 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_id IN () GROUP BY s.state, s.indicator_datum_id) AS LatestStateUpdate ON (IndicatorDatum.id = LatestStateUpdate.indicator_datum_id AND IndicatorDatum.state = LatestStateUpdate.state) 
INNER JOIN astest.indicator_datum_updates AS IndicatorDatumUpdate ON (IndicatorDatumUpdate.indicator_datum_id = LatestStateUpdate.indicator_datum_id AND IndicatorDatumUpdate.created = LatestStateUpdate.update_date) 
WHERE IndicatorDatum.id = (NULL)

我想知道我的代码有什么错误(阅读说明书后)我理解传递给保存方法的数组应该是这样的

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

【问题讨论】:

【参考方案1】:

您不应该在 select 中没有指定任何列,在这种情况下 s.state 和 s.indicator_datum_id 不会出现在 select 子句中。

【讨论】:

以上是关于数据库错误 Cakephp的主要内容,如果未能解决你的问题,请参考以下文章

Cakephp HABTM saveAll 创建错误的数据库记录

Cakephp mysql与远程数据库连接错误

CakePHP 错误的视图路径或 RoutingAliases 的对流

cakephp 没有将更新值保存到相关表,但没有错误

CakePHP 3 保存 BelongsToMany 关联未知类型“”错误

Cakephp 3.4 AJAX 请求抛出 403 禁止错误