为什么我必须使用find('list')连接表

时间:2014-09-25 14:57:09

标签: php cakephp cakephp-2.5

我的项目中有一件奇怪的事情。我有这样的联想。

Novelty->belongsTo->Category

这是模特:

App::uses('AppModel', 'Model');


class Category extends AppModel {

    public $actsAs = array('Tree','Containable');
    public $displayField = 'name';

    public $hasMany = array(
        'Novelty' => array(
            'className' => 'Novelty',
            'foreignKey' => 'category_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );
}


class Novelty extends AppModel {

    public $displayField = 'title';

    public $belongsTo = array(
        'Category' => array(
            'className' => 'Category',
            'foreignKey' => 'category_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

}

当我在模型文件Novelty.php中运行此类命令时,一切都顺利进行。

$this->find('all',[
    'conditions' => [
        'Category.lft >= ' => $mainCategory['Category']['lft'],
        'Category.rght <= ' => $mainCategory['Category']['rght']
    ],
]);

但如果我将模型文件从所有更改为列表,我会遇到以下异常:

$this->find('list',[
    'conditions' => [
        'Category.lft >= ' => $mainCategory['Category']['lft'],
        'Category.rght <= ' => $mainCategory['Category']['rght']
    ],
]);

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Category.lft' in 'where clause'
SQL Query: SELECT `Novelty`.`id` FROM `database`.`novelties` AS `Novelty` WHERE `Category`.`lft` >= 110 AND `Category`.`rght` <= 115 

在控制器中也是如此。

是一些错误,还是我项目的复杂性?我使用 CakePHP 2.5.1

0 个答案:

没有答案