SimpleTree

Simple Tree model trait

 trait Winter\Storm\Database\Traits\SimpleTree

Simple tree implementation, for advanced implementation see: Winter\Storm\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 \Winter\Storm\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';

Methods

public static bootSimpleTree ()

Returns
mixed

public getAll () : Winter\Storm\Database\Collection

Returns all nodes and children.

Returns

public getAllChildren () : Winter\Storm\Database\Collection

Get a list of children records, with their children (recursive)

Returns

public getChildCount () : int

Returns number of all children below it.

Returns
int

public getChildren () : Winter\Storm\Database\Collection

Returns direct child nodes.

Returns

public getParentColumnName () : string

Get parent column name.

Returns
string

public getParentId () : int

Get value of the model parent_id column.

Returns
int

public getQualifiedParentColumnName () : string

Get fully qualified parent column name.

Returns
string

public newCollection (array $models = [])

Return a custom TreeCollection collection

Parameters
Property Description
$models
array
Returns
mixed

public scopeGetAllRoot ($query) : Winter\Storm\Database\Collection

Returns a list of all root nodes, without eager loading.

Parameters
Property Description
$query
mixed
Returns

public scopeGetNested ($query) : Collection

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

Parameters
Property Description
$query
mixed
Returns

A collection

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

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

Parameters
Property Description
$query
mixed
$column
string

Array values

$key
string

Array keys

$indent
string

Character to indent depth

Returns
array
Copyright © 2024 Winter CMS