CAKEPHP自引用表

时间:2013-08-27 16:04:13

标签: cakephp cakephp-2.0

我有一个自定义的参考表: -

public $hasMany = array(
    'ChildCategory' => array(
        'className' => 'Category',
        'foreignKey' => 'parent_category_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);

public $belongsTo = array(
    'ParentCategory' => array(
        'className' => 'Category',
        'foreignKey' => 'parent_category_id',
        'conditions' => array('ParentCategory.parent_category_id' => '0'),
        'fields' => '',
        'order' => ''
    )
);

我们只有一个级别的父级孩子,因此树木很小ott。我正在尝试编写查找代码以获取仅用于添加编辑功能的父项列表。

    $parentCategories = $this->Category->ParentCategory->find('list');

但是sql不包含我的条件???

SELECT ParentCategoryidParentCategoryname FROM bidupcategories AS ParentCategory WHERE 1 = 1 < / p>

是否存在不包括该条件的原因?

1 个答案:

答案 0 :(得分:1)

$belongsTo关系的条件仅适用于从类别查询中检索ParentCategory时(​​假设您显示的关系仅在类别模型中)。

这意味着如果您在类别上执行find()并通过recursivecontain参数包含ParentCategory,或者可能使用read(),则会使用该条件。

当您直接在ParentCategory上查询模型中的条件被旁路时,您只是将该关系用作获取该模型的捷径,而无需直接加载或将其放入{{1 }}

正如user2076809建议的那样,如果您特别想在ParentCategory上使用列表,那么您最好的选择可能就是自己包含这个条件:

$uses
相关问题