Skip to main content
Rest Generic Class provides native support for hierarchical data structures like categories, organizational charts, menu trees, and comment threads. This guide shows you how to model, query, and display tree structures efficiently.

Enabling Hierarchy Support

To enable hierarchy features, define the HIERARCHY_FIELD_ID constant in your model:
Category.php
HIERARCHY_FIELD_ID must be the name of the self-referencing foreign key column (typically parent_id, manager_id, or role_id).

Hierarchy Helper Methods

Once HIERARCHY_FIELD_ID is defined, your model gains several helper methods:

List Endpoint Hierarchy Modes

The hierarchy parameter transforms flat lists into tree structures.

Mode: roots_only

Returns only root nodes (nodes with parent_id = null):
Response:

Mode: flat_all

Returns all nodes in a flat list (default SQL behavior):

Mode: with_descendants

Returns root nodes with nested children:
Response:

Mode: with_ancestors

Returns nodes with their ancestor chain:
Response shows the full path from root to the selected node:

Show Endpoint Hierarchy Modes

The show endpoint supports hierarchy modes when fetching a single record.

Mode: node_only

Returns just the node with an empty children array:
Response:

Mode: with_descendants (Default)

Returns the node with all its descendants:
Response:

Mode: with_ancestors

Returns the ancestor chain from root to this node:
Response:

Mode: full_branch

Returns ancestors + node + descendants (the complete branch):

Hierarchy Configuration Options

Without max_depth, deep trees can cause timeouts. Always set max_depth for large hierarchies or use pagination.

Combining Hierarchy with Filters

Filter the tree before building the hierarchy:
This:
  1. Filters categories where active = true
  2. Builds a tree with only active categories
  3. Limits depth to 2 levels

Pagination with Hierarchy

Paginate root nodes, not the entire tree:
This returns 10 root categories, each with up to 2 levels of descendants.
Pagination applies to root nodes only. Descendants are loaded in full (up to max_depth).

Real-World Examples

E-commerce Category Tree

Build a navigation menu:

Organizational Chart

Show company structure:

Nested Comment Thread

Display threaded comments:
Get the path from root to a specific node:
Response provides the complete breadcrumb chain:

Performance Optimization

Problem: N+1 Queries

Without optimization, loading a tree recursively triggers one query per node.

Solution: Optimized Loading

The package uses optimized queries:
The hierarchy mode uses breadth-first loading to minimize queries:
  1. Load all root nodes (1 query)
  2. Load all level-1 children (1 query)
  3. Load all level-2 children (1 query)
  4. Continue until max_depth
Total queries = max_depth + 1
For a tree with 1000 nodes and max_depth=3, this is 4 queries instead of 1000.

Indexing

Always index the hierarchy field:

Troubleshooting

Hierarchy Not Working

Symptom: 400 error “Model does not support hierarchical listing” Cause: HIERARCHY_FIELD_ID is not defined Solution:

Timeout on Large Trees

Symptom: Request times out with large hierarchies Causes:
  • No max_depth set
  • Very deep tree (100+ levels)
  • Missing index on hierarchy field
Solutions:
  • Set max_depth: "max_depth": 3
  • Use pagination on roots
  • Add database index on parent_id
  • Filter the dataset: "oper": {"and": ["active|=|true"]}

Circular References

Symptom: Infinite loop or stack overflow Cause: Data has circular references (A → B → A) Solution: Add database constraint to prevent circular references:

Empty Children Arrays

Symptom: Leaf nodes don’t have children property Cause: include_empty_children is false Solution:

Next Steps

Advanced Filtering

Combine hierarchy with complex filters

Relation Loading

Eager-load relations in hierarchical queries

Caching

Cache tree structures for faster responses

API Reference

Complete hierarchy API reference

Evidence

  • File: src/Core/Services/BaseService.php
    Lines: 679-754, 828-925, 936-1031
    Implements showHierarchy(), buildShowHierarchy(), loadDescendantsOptimized(), and loadAncestorsOptimized()
  • File: src/Core/Models/BaseModel.php
    Lines: 52-55, 169-240
    Defines HIERARCHY_FIELD_ID and helper methods (hasHierarchyField(), getHierarchyAncestors(), getHierarchyDescendants(), etc.)
  • File: documentacion/doc-en/03-usage/02-scenarios.md
    Lines: 76-104
    Shows hierarchy configuration example