Cakephp SQL错误:1064:

时间:2011-09-22 15:46:20

标签: php cakephp

在名为Attachment的表上进行查找调用,该表与另一个名为AddOn的表相关联。我收到一个SQL错误。它似乎正在寻找一个名为display_field的字段,但这不在表中。为什么要找它我不知道。

$previous = $this->AddOn->Attachment->find('first', array('conditions'=> array('model'=>'AddOn', 'foreign_key'=>$id)));

错误:

SQL Error: 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'display' at line 1 [CORE/cake/libs/model/datasources/dbo_source.php, line 684]

Query: display

Array
(
    [Attachment] => Array
        (
            [id] => 44
            [model] => AddOn
            [foreign_key] => 41
            [dirname] => img
            [basename] => fontgame_620x432.png
            [checksum] => 0488620c807cfaebb17489cb9c33f4fe
            [alternative] => Fontgame-620x432
            [group] => attachment
            [created] => 2011-09-22 14:04:35
            [modified] => 2011-09-22 14:04:35
            [file] => /home/t553788/public_html/res360/res/app/webroot/Media/transfer/img/fontgame_620x432.png
            [ratio] => 1.43518518519
            [known_ratio] => √2:1
            [megapixel] => 0
            [quality] => 1
            [width] => 620
            [height] => 432
            [bits] => 8
            [size] => 176726
            [mime_type] => image/png
        )

    [AddOn] => Array
        (
            [id] => 41
            [add_on_category_id] => 6
            [title] => new addon
            [description] => qwedwed
            [enabled] => 0
            [list_no] => 0
            [availability] => 1
            [price_quote_as] => 0
            [price] => 10.00
            [valid_from] => 2011-09-01
            [valid_to] => 2011-09-02
            [created] => 2011-09-20 18:22:51
            [updated] => 2011-09-22 16:29:38
            [deleted] => 0
            [display_field] => *missing*
        )

)

附件模型:

        <?php
        class Attachment extends AppModel {

            var $name = 'Attachment';

            var $actsAs = array('Containable');

        }
        ?>

Addon Model;

            <?php
            class AddOn extends AppModel {

                var $name = 'AddOn';

                var $actsAs = array('Containable');

                var $belongsTo = array(

                    'AddOnCategory' => array('className' => 'AddOnCategory')

                );

                var $hasMany = array(

                    'AddOnPurchase' => array('className' => 'AddOnPurchase'),
                    'AddOnRateroom' => array('className' => 'AddOnRateroom'),

                    'Attachment' => array(
                          'className' => 'Media.Attachment',
                          'foreignKey' => 'foreign_key',
                          'conditions' => array('Attachment.model' => 'AddOn'),
                          'dependent' => true
                          )
                );

                /*
                var $validate = array(
                    'file' => array('mimeType' => array('rule' => array('checkMimeType', false, array( 'image/jpeg', 'image/png'))))
                );*/

                // before find  
                function beforeFind($queryData){

                    if(!isset($queryData['conditions']['AddOn.deleted'])){
                        $queryData['conditions']['AddOn.deleted'] = 0;
                    } 

                    return $queryData;

                }


            }
            ?>

1 个答案:

答案 0 :(得分:4)

正在执行的SQL查询是:display

如您所知,您正在寻找的SQL查询应该从:SELECT <something> FROM ...

开始

这个显然没有,这就是MySQL试图告诉你的。

检查您是否从任何代码中调用了名为display()的方法:

$this->display(); // in models or behaviors
$this->Attachment->display(); // in controller, etc

当您在模型上调用缺少的方法时,CakePHP会执行此操作。

一个常见的罪魁祸首是,当实际调用正确的属性$actAs时,通过设置模型属性$actsAs来包含行为。