October\Rain\Database\Traits\NestedTree

Source: ~/vendor/october/rain/src/Database/Traits/NestedTree.php

Nested set model trait

Model table must have parent_id, nest_left, nest_right and nest_depth table columns. In the model class definition:

use \October\Rain\Database\Traits\NestedTree;

$table->integer('parent_id')->nullable(); $table->integer('nest_left')->nullable(); $table->integer('nest_right')->nullable(); $table->integer('nest_depth')->nullable();

You can change the column names used by declaring:

const PARENT_ID = 'my_parent_column'; const NEST_LEFT = 'my_left_column'; const NEST_RIGHT = 'my_right_column'; const NEST_DEPTH = 'my_depth_column';

General access methods:

$model->getRoot(); // Returns the highest parent of a node. $model->getRootList(); // Returns an indented array of key and value columns from root. $model->getParent(); // The direct parent node. $model->getParents(); // Returns all parents up the tree. $model->getParentsAndSelf(); // Returns all parents up the tree and self. $model->getChildren(); // Set of all direct child nodes. $model->getSiblings(); // Return all siblings (parent's children). $model->getSiblingsAndSelf(); // Return all siblings and self. $model->getLeaves(); // Returns all final nodes without children. $model->getDepth(); // Returns the depth of a current node. $model->getChildCount(); // Returns number of all children.

Query builder methods:

$query->withoutNode(); // Filters a specific node from the results. $query->withoutSelf(); // Filters current node from the results. $query->withoutRoot(); // Filters root from the results. $query->children(); // Filters as direct children down the tree. $query->allChildren(); // Filters as all children down the tree. $query->parent(); // Filters as direct parent up the tree. $query->parents(); // Filters as all parents up the tree. $query->siblings(); // Filters as all siblings (parent's children). $query->leaves(); // Filters as all final nodes without children. $query->getNested(); // Returns an eager loaded collection of results. $query->listsNested(); // Returns an indented array of key and value columns.

Flat result access methods:

$model->getAll(); // Returns everything in correct order. $model->getAllRoot(); // Returns all root nodes. $model->getAllChildren(); // Returns all children down the tree. $model->getAllChildrenAndSelf(); // Returns all children and self.

Eager loaded access methods:

$model->getEagerRoot(); // Returns a list of all root nodes, with ->children eager loaded. $model->getEagerChildren(); // Returns direct child nodes, with ->children eager loaded.

Protected properties

protected int $moveToNewParentId

Indicates if the model should be aligned to new parent.

Public methods

public static void bootNestedTree()

public void deleteDescendants()

Deletes a branch off the tree, shifting all the elements on the right back to the left so the counts work.

public October\Rain\Database\Collection getAll($columns=array( 0=>'*'))

Returns all nodes and children.

Parameters
  • $columns

public October\Rain\Database\Collection getAllChildren()

Returns all children down the tree.

public October\Rain\Database\Collection getAllChildrenAndSelf()

Returns all children and self.

public int getChildCount()

Returns number of all children below it.

public October\Rain\Database\Collection getChildren()

Returns direct child nodes.

public int getDepth()

Get value of the depth column.

public string getDepthColumnName()

Get depth column name.

public October\Rain\Database\Collection getEagerChildren()

Returns direct child nodes, with ->children eager loaded.

public October\Rain\Database\Collection getEagerRoot()

Returns a list of all root nodes, with children eager loaded.

public October\Rain\Database\Collection getLeaves()

Returns all final nodes without children.

public int getLeft()

Get value of the left column.

public string getLeftColumnName()

Get left column name.

public October\Rain\Database\Model getLeftSibling()

Return left sibling

public int getLevel()

Returns the level of this node in the tree. Root level is 0.

public October\Rain\Database\Collection getParent()

The direct parent node.

public string getParentColumnName()

Get parent column name.

public int getParentId()

Get value of the model parent_id column.

public October\Rain\Database\Collection getParents()

Returns all parents up the tree.

public October\Rain\Database\Collection getParentsAndSelf()

Returns all parents up the tree and self.

public string getQualifiedDepthColumnName()

Get fully qualified depth column name.

public string getQualifiedLeftColumnName()

Get fully qualified left column name.

public string getQualifiedParentColumnName()

Get fully qualified parent column name.

public string getQualifiedRightColumnName()

Get fully qualified right column name.

public int getRight()

Get value of the right column.

public string getRightColumnName()

Get right column name.

public October\Rain\Database\Model getRightSibling()

Return right sibling

public October\Rain\Database\Model getRoot()

Returns the root node starting from the current node.

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

Returns an array column/key pair of all root nodes, with children eager loaded.

Parameters
  • $column
  • $key
  • $indent

public October\Rain\Database\Collection getSiblings()

Return all siblings (parent's children).

public October\Rain\Database\Collection getSiblingsAndSelf()

Return all siblings and self.

public boolean isChild()

Returns true if this is a child node.

public boolean isDescendantOf(NestedSet $other)

Returns true if node is a descendant.

Parameters
  • NestedSet $other

public boolean isInsideSubtree(Model $node)

Checks if the supplied node is inside the subtree of this model.

Parameters
  • Model $node

public boolean isLeaf()

Returns true if this is a leaf node (end of a branch).

public boolean isRoot()

Returns true if this is a root node.

public October\Rain\Database\Model makeChildOf($node)

Make model node a child of specified node.

Parameters
  • $node

public October\Rain\Database\Model makeRoot()

Make this model a root node.

public October\Rain\Database\Model moveAfter($node)

Move to the model to after (right) a specified node.

Parameters
  • $node

public October\Rain\Database\Model moveBefore($node)

Move to the model to before (left) specified node.

Parameters
  • $node

public October\Rain\Database\Model moveLeft()

Find the left sibling and move to left of it.

public October\Rain\Database\Model moveRight()

Find the right sibling and move to the right of it.

public void moveToNewParent()

If the parent identifier is dirty, realign the nesting.

public void newCollection($models=array())

Return a custom TreeCollection collection

Parameters
  • $models

public void restoreDescendants()

Restores all of the current node descendants.

public Illuminate\Database\Query\Builder scopeAllChildren($query, $includeSelf=false)

Set of all children & nested children.

Parameters
  • $query
  • $includeSelf

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

Returns a list of all root nodes, without eager loading

Parameters
  • $query

public Collection scopeGetNested($query)

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

Parameters
  • $query

public Illuminate\Database\Query\Builder scopeLeaves($query)

Returns all final nodes without children.

Parameters
  • $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.

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

public Illuminate\Database\Eloquent\Builder scopeParents($query, $includeSelf=false)

Returns a prepared query with all parents up the tree.

Parameters
  • $query
  • $includeSelf

public Illuminate\Database\Eloquent\Builder scopeSiblings($query, $includeSelf=false)

Filter targeting all children of the parent, except self.

Parameters
  • $query
  • $includeSelf

public Illuminate\Database\Query\Builder scopeWithoutNode($query, $node)

Query scope which extracts a certain node object from the current query expression.

Parameters
  • $query
  • $node

public Illuminate\Database\Query\Builder scopeWithoutRoot($query)

Extracts first root (from the current node context) from current query expression.

Parameters
  • $query

public Illuminate\Database\Query\Builder scopeWithoutSelf($query)

Extracts current node (self) from current query expression.

Parameters
  • $query

public void setDefaultLeftAndRight()

Set defaults for left and right columns.

public October\Rain\Database\Model setDepth()

Sets the depth attribute

public void shiftSiblingsForRestore()

Allocates a slot for the the current node between its siblings.

public void storeNewParent()

Handle if the parent column is modified so it can be realigned.

Protected methods

protected int getOtherBoundary($node, $target, $position)

Calculates the other boundary.

Parameters
  • $node
  • $target
  • $position

protected int getPrimaryBoundary($node, $target, $position)

Calculates the boundary.

Parameters
  • $node
  • $target
  • $position

protected array getSortedBoundaries($node, $target, $position)

Calculates a sorted boundaries array.

Parameters
  • $node
  • $target
  • $position

protected October\Rain\Database\Model moveTo(mixed $target, string $position)

Handler for all node alignments.

Parameters
  • mixed $target
  • string $position

protected int performMove($node, $target, $position)

Executes the SQL query associated with the update of the indexes affected by the move operation.

Parameters
  • $node
  • $target
  • $position

protected void validateMove($node, $target, $position)

Validates a proposed move and returns true if changes are needed.

Parameters
  • $node
  • $target
  • $position