SimpleTree model trait

Simple tree implementation, for advanced implementation see: October\Rain\Database\Traits\NestedTree

SimpleTree is the bare minimum needed for tree functionality, the methods defined here should be implemented by all "tree" traits.


Model table must have parent_id table column. In the model class definition:

use \October\Rain\Database\Traits\SimpleTree;

General access methods:

$model->getChildren(); // Returns children of this node $model->getChildCount(); // Returns number of all children. $model->getAllChildren(); // Returns all children of this node $model->getAllRoot(); // Returns all root level nodes (eager loaded) $model->getAll(); // Returns everything in correct order.

Query builder methods:

$query->listsNested(); // Returns an indented array of key and value columns.

You can change the sort field used by declaring:

const PARENT_ID = 'my_parent_column';

Public Methods

public getAll()

public getAll(): October\Rain\Database\Collection 

getAll returns all nodes and children.

public getAllChildren()

public getAllChildren(): October\Rain\Database\Collection 

getAllChildren gets a list of children records, with their children (recursive)

public getChildCount()

public getChildCount(): int 

getChildCount returns number of all children below it.

public getChildren()

public getChildren(): October\Rain\Database\Collection 

getChildren returns direct child nodes.

public getParentColumnName()

public getParentColumnName(): string 

public getParentId()

public getParentId(): int 

getParentId gets value of the model parent_id column.

public getParents()

public getParents(): void

getParents returns an array of parents, this is a heavy query and can produce in multiple queries.

public getQualifiedParentColumnName()

public getQualifiedParentColumnName(): string 

public initializeSimpleTree()

public initializeSimpleTree(): void

initializeSimpleTree constructor

public newCollection()

public newCollection($models = []): void

newCollection returns a custom TreeCollection collection.

public scopeGetAllRoot()

public scopeGetAllRoot($query): October\Rain\Database\Collection 

scopeGetAllRoot returns a list of all root nodes, without eager loading.

public scopeGetNested()

public scopeGetNested($query): Collection 

scopeGetNested is a non-chaining scope, returns an eager loaded hierarchy tree. Children are eager loaded inside the $model->children relation.

public scopeListsNested()

public scopeListsNested(
    string $column,
    string $key,
    string $indent = null,
    $indent = '   '
): array 

scopeListsNested gets an array with values of a given column. Values are indented according to their depth.