【问题标题】:Get parent node in Tree cakephp 3在 Tree cakephp 3 中获取父节点
【发布时间】:2018-09-07 22:44:28
【问题描述】:

假设我有一个名为 categories 的表,它在 CakePHP 3 应用程序中使用 Tree Behavior。如果给我一个 Category id,是否有允许我检查 Category 是否为父 Category 的函数或无需执行多个查找查询即可获取节点的父 Category 的函数?

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

感谢您的帮助

【问题讨论】:

    标签: php cakephp php-5.6 cakephp-3.6


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      根据Cakephp 3

      已经有一个路径查找器可用于查找特定节点/ID 的完整路径。 使用它你可以得到如下的父节点:

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

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

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 2019-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多