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.


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 static void bootSimpleTree()

public October\Rain\Database\Collection getAll()

Returns all nodes and children.

public October\Rain\Database\Collection getAllChildren()

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

public int getChildCount()

Returns number of all children below it.

public October\Rain\Database\Collection getChildren()

Returns direct child nodes.

public string getParentColumnName()

Get parent column name.

public int getParentId()

Get value of the model parent_id column.

public string getQualifiedParentColumnName()

Get fully qualified parent column name.

public void newCollection($models=array())

Return a custom TreeCollection collection

  • $models

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

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

  • $query

public Collection scopeGetNested($query)

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

  • $query

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

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

  • string $column - Array values
  • string $key - Array keys
  • string $indent - Character to indent depth
  • $indent