SimpleTree
Simple Tree 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.
Usage:
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';
Table of Contents
- bootSimpleTree() : mixed
- getAll() : Collection
- Returns all nodes and children.
- getAllChildren() : Collection
- Get a list of children records, with their children (recursive)
- getChildCount() : int
- Returns number of all children below it.
- getChildren() : Collection
- Returns direct child nodes.
- getParentColumnName() : string
- Get parent column name.
- getParentId() : int
- Get value of the model parent_id column.
- getQualifiedParentColumnName() : string
- Get fully qualified parent column name.
- newCollection() : mixed
- Return a custom TreeCollection collection
- scopeGetAllRoot() : Collection
- Returns a list of all root nodes, without eager loading.
- scopeGetNested() : Collection
- Non chaining scope, returns an eager loaded hierarchy tree. Children are eager loaded inside the $model->children relation.
- scopeListsNested() : array<string|int, mixed>
- Gets an array with values of a given column. Values are indented according to their depth.
Methods
bootSimpleTree()
public
static bootSimpleTree() : mixed
Return values
mixed —getAll()
Returns all nodes and children.
public
getAll() : Collection
Return values
Collection —getAllChildren()
Get a list of children records, with their children (recursive)
public
getAllChildren() : Collection
Return values
Collection —getChildCount()
Returns number of all children below it.
public
getChildCount() : int
Return values
int —getChildren()
Returns direct child nodes.
public
getChildren() : Collection
Return values
Collection —getParentColumnName()
Get parent column name.
public
getParentColumnName() : string
Return values
string —getParentId()
Get value of the model parent_id column.
public
getParentId() : int
Return values
int —getQualifiedParentColumnName()
Get fully qualified parent column name.
public
getQualifiedParentColumnName() : string
Return values
string —newCollection()
Return a custom TreeCollection collection
public
newCollection([array<string|int, mixed> $models = [] ]) : mixed
Parameters
- $models : array<string|int, mixed> = []
Return values
mixed —scopeGetAllRoot()
Returns a list of all root nodes, without eager loading.
public
scopeGetAllRoot(mixed $query) : Collection
Parameters
- $query : mixed
Return values
Collection —scopeGetNested()
Non chaining scope, returns an eager loaded hierarchy tree. Children are eager loaded inside the $model->children relation.
public
scopeGetNested(mixed $query) : Collection
Parameters
- $query : mixed
Return values
Collection —A collection
scopeListsNested()
Gets an array with values of a given column. Values are indented according to their depth.
public
scopeListsNested(mixed $query, string $column[, string $key = null ][, string $indent = ' ' ]) : array<string|int, mixed>
Parameters
- $query : mixed
- $column : string
-
Array values
- $key : string = null
-
Array keys
- $indent : string = ' '
-
Character to indent depth