在Tree cakephp 3中获取父节点

问题描述 投票:0回答:2

假设我有一个名为categories的表,它在CakePHP 3应用程序中使用Tree Behavior。如果我有一个类别ID,是否有函数允许我检查类别是父类别还是函数来获取节点的父类别而不必进行多个查找查询?

我在网上找不到任何东西。

谢谢你的帮助

php cakephp php-5.6 cakephp-3.6
2个回答
1
投票

TreeBehavior使用parent_id字段,因此您可以准备名为ParentCategoriesChildrenCategories的关系。

$this->belongsTo('ParentCategories', [
    'className' => 'Categories',
    'foreignKey' => 'parent_id',
]);

$this->hasMany('ChildrenCategories', [
    'className' => 'Categories',
    'foreignKey' => 'parent_id',
]);

0
投票

根据Cakephp 3

已有路径查找器可用于查找特定节点/ ID的完整路径。使用此方法,您可以获得父节点,如下所示:

$completePath = $this->Model->find('path', ['for' => $category_id])->first();
$parentNode = $completePath['id']; 

进一步参考:https://github.com/cakephp/cakephp/issues/12539

希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.