CAKEPHP自引用表
我有一个定义如下的自参考表:-CAKEPHP自引用表,cakephp,cakephp-2.0,Cakephp,Cakephp 2.0,我有一个定义如下的自参考表:- public $hasMany = array( 'ChildCategory' => array( 'className' => 'Category', 'foreignKey' => 'parent_category_id', 'dependent' => false, 'conditions' => '', 'fields' => ''
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' => ''
)
);
我们只有一个级别的父子关系,所以一棵树很简单。我正在尝试对查找进行编码,以获取仅用于添加编辑功能的父级列表
$parentCategories = $this->Category->ParentCategory->find('list');
但是sql不包含我的条件
选择ParentCategory
id
,ParentCategory
name
从bidup
categories
作为ParentCategory
,其中1=1
是否有不包括该条件的原因?仅当从类别查询中检索父类别时,
$belongsTo
关系上的条件才适用(假设您显示的关系仅在类别模型中)
这意味着如果您通过递归
或包含
参数对类别和包含的父类别执行了查找()
,或者可能使用了读取()
,则将使用该条件
当您直接在ParentCategory上查询模型中的条件被旁路时,您只是使用该关系作为到达该模型的捷径,而不必直接加载它或将它放入$uses
正如user2076809所建议的,如果您特别想使用ParentCategory上的列表,那么您最好的选择可能包括以下条件:
$parentCategories = $this->Category->ParentCategory->find(
'list',
array(
'conditions' => array('ParentCategory.parent_category_id' => 0),
)
);
我对模型关系的条件不太熟悉,但它可能只适用于联接。您可以通过执行以下操作来获得所需内容:
$parentCategories=$this->Category->ParentCategory->find('list','conditions'=>array('parent\u Category\u id'=>0))代码>