NestedTree
Nested set model trait
trait Winter\Storm\Database\Traits\NestedTree
Model table must have parent_id, nest_left, nest_right and nest_depth table columns. In the model class definition:
use \Winter\Storm\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.
Properties
protected
$moveToNewParentId
:
int
= null
Indicates if the model should be aligned to new parent.
Methods
public static bootNestedTree ()
public deleteDescendants () : void
Deletes a branch off the tree, shifting all the elements on the right back to the left so the counts work.
public
getAll (array $columns = ["*"]
)
: Winter\Storm\Database\Collection
Returns all nodes and children.
Property | Type | Description |
---|---|---|
$columns | array |
array
|
public getAllChildren () : Winter\Storm\Database\Collection
public getAllChildrenAndSelf () : Winter\Storm\Database\Collection
public getChildCount () : int
Returns number of all children below it.
public getChildren () : Winter\Storm\Database\Collection
public getDepth () : int
Get value of the depth column.
public getDepthColumnName () : string
Get depth column name.
public getEagerChildren () : Winter\Storm\Database\Collection
public getEagerRoot () : Winter\Storm\Database\Collection
Returns a list of all root nodes, with children eager loaded.
public getLeaves () : Winter\Storm\Database\Collection
public getLeft () : int
Get value of the left column.
public getLeftColumnName () : string
Get left column name.
public getLeftSibling () : Winter\Storm\Database\Model
public getLevel () : int
Returns the level of this node in the tree.
Root level is 0.
public getParent () : Winter\Storm\Database\Model
public getParentColumnName () : string
Get parent column name.
public getParentId () : int
Get value of the model parent_id column.
public getParents () : Winter\Storm\Database\Collection
public getParentsAndSelf () : Winter\Storm\Database\Collection
public getQualifiedDepthColumnName () : string
Get fully qualified depth column name.
public getQualifiedLeftColumnName () : string
Get fully qualified left column name.
public getQualifiedParentColumnName () : string
Get fully qualified parent column name.
public getQualifiedRightColumnName () : string
Get fully qualified right column name.
public getRight () : int
Get value of the right column.
public getRightColumnName () : string
Get right column name.
public getRightSibling () : Winter\Storm\Database\Model
public getRoot () : Winter\Storm\Database\Model
public
getRootList ($column, $key = null
, string $indent = " "
)
: array
Returns an array column/key pair of all root nodes, with children eager loaded.
Property | Type | Description |
---|---|---|
$column | mixed |
mixed
|
$key | mixed |
mixed
|
$indent | string |
string
|
public getSiblings () : Winter\Storm\Database\Collection
public getSiblingsAndSelf () : Winter\Storm\Database\Collection
public isChild () : bool
Returns true if this is a child node.
public isDescendantOf ($other) : bool
Returns true if node is a descendant.
Property | Type | Description |
---|---|---|
$other | mixed |
mixed
|
public isInsideSubtree (Winter\Storm\Database\Model $node) : bool
Checks if the supplied node is inside the subtree of this model.
Property | Type | Description |
---|---|---|
$node | Winter\Storm\Database\Model |
public isLeaf () : bool
Returns true if this is a leaf node (end of a branch).
public isRoot () : bool
Returns true if this is a root node.
public makeChildOf ($node) : Winter\Storm\Database\Model
Make model node a child of specified node.
Property | Type | Description |
---|---|---|
$node | mixed |
mixed
|
public makeRoot () : Winter\Storm\Database\Model
public moveAfter ($node) : Winter\Storm\Database\Model
Move to the model to after (right) a specified node.
Property | Type | Description |
---|---|---|
$node | mixed |
mixed
|
public moveBefore ($node) : Winter\Storm\Database\Model
Move to the model to before (left) specified node.
Property | Type | Description |
---|---|---|
$node | mixed |
mixed
|
public moveLeft () : Winter\Storm\Database\Model
public moveRight () : Winter\Storm\Database\Model
public moveToNewParent () : void
If the parent identifier is dirty, realign the nesting.
public
newCollection (array $models = []
)
: TreeCollection
Return a custom TreeCollection collection
Property | Type | Description |
---|---|---|
$models | array |
array
|
public restoreDescendants () : void
Restores all of the current node descendants.
public
scopeAllChildren (Illuminate\Database\Query\Builder $query, boolean $includeSelf = false
)
: Illuminate\Database\Query\Builder
Set of all children & nested children.
Property | Type | Description |
---|---|---|
$query | Illuminate\Database\Query\Builder |
Illuminate\Database\Query\Builder
|
$includeSelf | boolean |
boolean
|
public scopeGetAllRoot (Illuminate\Database\Query\Builder $query) : Winter\Storm\Database\Collection
Returns a list of all root nodes, without eager loading
Property | Type | Description |
---|---|---|
$query | Illuminate\Database\Query\Builder |
Illuminate\Database\Query\Builder
|
public scopeGetNested (Illuminate\Database\Query\Builder $query) : Collection
Non chaining scope, returns an eager loaded hierarchy tree. Children are eager loaded inside the $model->children relation.
Property | Type | Description |
---|---|---|
$query | Illuminate\Database\Query\Builder |
Illuminate\Database\Query\Builder
|
A collection
public scopeLeaves (Illuminate\Database\Query\Builder $query) : Illuminate\Database\Query\Builder
Returns all final nodes without children.
Property | Type | Description |
---|---|---|
$query | Illuminate\Database\Query\Builder |
Illuminate\Database\Query\Builder
|
public
scopeListsNested (Illuminate\Database\Query\Builder $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.
Property | Type | Description |
---|---|---|
$query | Illuminate\Database\Query\Builder |
Illuminate\Database\Query\Builder
|
$column | string |
string
Array values |
$key | string |
string
Array keys |
$indent | string |
string
Character to indent depth |
public
scopeParents (Illuminate\Database\Query\Builder $query, boolean $includeSelf = false
)
: Illuminate\Database\Eloquent\Builder
Returns a prepared query with all parents up the tree.
Property | Type | Description |
---|---|---|
$query | Illuminate\Database\Query\Builder |
Illuminate\Database\Query\Builder
|
$includeSelf | boolean |
boolean
|
public
scopeSiblings (Illuminate\Database\Query\Builder $query, boolean $includeSelf = false
)
: Illuminate\Database\Eloquent\Builder
Filter targeting all children of the parent, except self.
Property | Type | Description |
---|---|---|
$query | Illuminate\Database\Query\Builder |
Illuminate\Database\Query\Builder
|
$includeSelf | boolean |
boolean
|
public scopeWithoutNode (Illuminate\Database\Query\Builder $query, $node) : Illuminate\Database\Query\Builder
Query scope which extracts a certain node object from the current query expression.
Property | Type | Description |
---|---|---|
$query | Illuminate\Database\Query\Builder |
Illuminate\Database\Query\Builder
|
$node | mixed |
mixed
|
public scopeWithoutRoot (Illuminate\Database\Query\Builder $query) : Illuminate\Database\Query\Builder
Extracts first root (from the current node context) from current query expression.
Property | Type | Description |
---|---|---|
$query | Illuminate\Database\Query\Builder |
Illuminate\Database\Query\Builder
|
public scopeWithoutSelf (Illuminate\Database\Query\Builder $query) : Illuminate\Database\Query\Builder
Extracts current node (self) from current query expression.
Property | Type | Description |
---|---|---|
$query | Illuminate\Database\Query\Builder |
Illuminate\Database\Query\Builder
|
public setDefaultLeftAndRight () : void
Set defaults for left and right columns.
public setDepth () : Winter\Storm\Database\Model
public shiftSiblingsForRestore () : void
Allocates a slot for the the current node between its siblings.
public storeNewParent () : void
Handle if the parent column is modified so it can be realigned.
protected getOtherBoundary (Winter\Storm\Database\Model $node, Winter\Storm\Database\Model $target, string $position) : int
Calculates the other boundary.
Property | Type | Description |
---|---|---|
$node | Winter\Storm\Database\Model |
The node to be moved |
$target | Winter\Storm\Database\Model |
The target node to be moved relative to |
$position | string |
string
One of the following values: child, left, right |
protected getPrimaryBoundary (Winter\Storm\Database\Model $node, Winter\Storm\Database\Model $target, string $position) : int | null
Calculates the boundary.
Property | Type | Description |
---|---|---|
$node | Winter\Storm\Database\Model |
The node to be moved |
$target | Winter\Storm\Database\Model |
The target node to be moved relative to |
$position | string |
string
One of the following values: child, left, right |
protected getSortedBoundaries (Winter\Storm\Database\Model $node, Winter\Storm\Database\Model $target, string $position) : array
Calculates a sorted boundaries array.
Property | Type | Description |
---|---|---|
$node | Winter\Storm\Database\Model |
The node to be moved |
$target | Winter\Storm\Database\Model |
The target node to be moved relative to |
$position | string |
string
One of the following values: child, left, right |
protected moveTo (mixed $target, string $position) : Winter\Storm\Database\Model
Handler for all node alignments.
Property | Type | Description |
---|---|---|
$target | mixed |
mixed
The ID or model instance of the target node |
$position | string |
string
One of the following values: child, left, right |
protected performMove (Winter\Storm\Database\Model $node, Winter\Storm\Database\Model $target, string $position) : int
Executes the SQL query associated with the update of the indexes affected by the move operation.
Property | Type | Description |
---|---|---|
$node | Winter\Storm\Database\Model |
The node to be moved |
$target | Winter\Storm\Database\Model |
The target node to be moved relative to |
$position | string |
string
One of the following values: child, left, right |
protected validateMove (Winter\Storm\Database\Model $node, Winter\Storm\Database\Model $target, string $position) : bool
Validates a proposed move and returns true if changes are needed.
Property | Type | Description |
---|---|---|
$node | Winter\Storm\Database\Model |
The node to be moved |
$target | Winter\Storm\Database\Model |
The target node to be moved relative to |
$position | string |
string
One of the following values: child, left, right |