staudenmeir/laravel-adjacency-list

Path and custom paths are not consistent

AntonioDiPassio-AppSys opened this issue · 3 comments

I expect the path (or a custom path) to always give the same result, but it does not.
In the docs it says that the path is from the parent to the model.

I have this custom path:

 public function getCustomPaths()
    {
        return [
            [
                'name' => 'slug_path',
                'column' => 'slug',
                'separator' => '/',
            ],
        ];
    }

And I have these models:

> Category::tree()->find(108)
= App\Models\Category {#7598
    id: 108,
    parent_id: 106,
    name: "seating furnitures",
    slug: "seating-furnitures",
    order: 20,
    created_at: null,
    updated_at: null,
    depth: 2,
    path: "99.106.108",
    slug_path: "2nd-hand/furniture/seating-furnitures",
  }

> Category::tree()->find(106)
= App\Models\Category {#7572
    id: 106,
    parent_id: 99,
    name: "furniture",
    slug: "furniture",
    order: 20,
    created_at: null,
    updated_at: null,
    depth: 1,
    path: "99.106",
    slug_path: "2nd-hand/furniture",
  }

> Category::tree()->find(99)
= App\Models\Category {#7593
    id: 99,
    parent_id: null,
    name: "2nd hand",
    slug: "2nd-hand",
    order: 3,
    created_at: null,
    updated_at: null,
    depth: 0,
    path: "99",
    slug_path: "2nd-hand",
  }

But this produces a different path for the parent:

> Category::tree()->find(108)->rootAncestor
= App\Models\Category {#7616
    id: 99,
    parent_id: null,
    name: "2nd hand",
    slug: "2nd-hand",
    order: 3,
    created_at: null,
    updated_at: null,
    depth: -2,
    path: "106.99",
    slug_path: "furniture/2nd-hand",
  }

Another example:

> foreach(Category::tree()->find(99)->descendantsAndSelf as $category){ echo "$category->id: $category->slug_path" . PHP_EOL; }
99: 2nd-hand
100: 2nd-hand/decoration
106: 2nd-hand/furniture
101: 2nd-hand/decoration/cameras
102: 2nd-hand/decoration/frames-and-paintings
103: 2nd-hand/decoration/pillows
104: 2nd-hand/decoration/mirrors
105: 2nd-hand/decoration/telephones-and-tv
107: 2nd-hand/furniture/shop-interior
108: 2nd-hand/furniture/seating-furnitures
116: 2nd-hand/furniture/chairs
115: 2nd-hand/furniture/seating-furnitures/sofas
117: 2nd-hand/decoration/mirrors/

> foreach(Category::tree()->find(99)->descendants as $category){ echo "$category->id: $category->slug_path" . PHP_EOL; }
100: decoration
106: furniture
101: decoration/cameras
102: decoration/frames-and-paintings
103: decoration/pillows
104: decoration/mirrors
105: decoration/telephones-and-tv
107: furniture/shop-interior
108: furniture/seating-furnitures
116: furniture/chairs
115: furniture/seating-furnitures/sofas
117: decoration/mirrors/

Hi @AntonioDiPassio-AppSys,

The paths always depend on the query you are running:

from the query's parent to the model

That's why they are different for tree queries compared to ancestor/rootAncestor queries.

In that case, isn't this path incorrect then? 2nd-hand is a parent of furniture and not the other way around

> Category::tree()->find(108)->rootAncestor
= App\Models\Category {#7616
    id: 99,
    parent_id: null,
    name: "2nd hand",
    slug: "2nd-hand",
    order: 3,
    created_at: null,
    updated_at: null,
    depth: -2,
    path: "106.99",
    slug_path: "furniture/2nd-hand",
  }

Also, it would be nice to have an 'absolute' path which would always start from the rootAncestor. Maybe you can add an option to the getCustomPaths(): 'absolute' => true

In that case, isn't this path incorrect then? 2nd-hand is a parent of furniture and not the other way around
Category::tree()->find(108)->rootAncestor

The path "order" also depends on the query's parent: Your rootAncestor query first reaches 106 (furniture) and then 99 (2nd hand).

Also, it would be nice to have an 'absolute' path which would always start from the rootAncestor. Maybe you can add an option to the getCustomPaths(): 'absolute' => true

That's not possible, unfortunately. The generation of absolute paths would work completely differently and can't be integrated like this.