【发布时间】:2018-09-07 22:44:28
【问题描述】:
假设我有一个名为 categories 的表,它在 CakePHP 3 应用程序中使用 Tree Behavior。如果给我一个 Category id,是否有允许我检查 Category 是否为父 Category 的函数或无需执行多个查找查询即可获取节点的父 Category 的函数?
我在网上找不到任何东西。
感谢您的帮助
【问题讨论】:
标签: php cakephp php-5.6 cakephp-3.6
假设我有一个名为 categories 的表,它在 CakePHP 3 应用程序中使用 Tree Behavior。如果给我一个 Category id,是否有允许我检查 Category 是否为父 Category 的函数或无需执行多个查找查询即可获取节点的父 Category 的函数?
我在网上找不到任何东西。
感谢您的帮助
【问题讨论】:
标签: php cakephp php-5.6 cakephp-3.6
TreeBehavior 使用parent_id 字段,因此您可以准备名为ParentCategories 和ChildrenCategories 的关系。
$this->belongsTo('ParentCategories', [
'className' => 'Categories',
'foreignKey' => 'parent_id',
]);
$this->hasMany('ChildrenCategories', [
'className' => 'Categories',
'foreignKey' => 'parent_id',
]);
【讨论】:
已经有一个路径查找器可用于查找特定节点/ID 的完整路径。 使用它你可以得到如下的父节点:
$completePath = $this->Model->find('path', ['for' => $category_id])->first();
$parentNode = $completePath['id'];
进一步参考:https://github.com/cakephp/cakephp/issues/12539
希望这会有所帮助!
【讨论】: