TreeTrait
Resolves a `TreeConfig` (property names plus scope) to database columns and a table via Doctrine metadata and hands over to the repository, which runs set-based nested-set SQL.
Properties are resolved strictly through metadata (whitelist): scalar field to column, to-one association to join column. An unknown property is an error, because column names from client input are never concatenated into SQL.
Table of Contents
Methods
- closeTreeGap() : Result
- Closes the gap in `lft`/`rgt` left by a deleted subtree.
- getTreeSubtree() : Result
- Returns ids of the whole subtree of a node plus its `lft`/`rgt` bounds (the basis for deleting the subtree and closing the gap afterwards).
- insertTreeNode() : Result
- Computes `lft`/`rgt`/`level` of a newly inserted node (after it is saved with `parent_id`); resolves columns via metadata and hands over to the repository ({@see \App\Core\Traits\Api\Models\Repositories\TreeTrait::insertTreeNode()}).
- inTreeLock() : Result
- Runs the operation under the tree lock of the given `TreeConfig` (table plus scope).
- moveSubtree() : Result
- Moves a whole subtree under another parent; resolves columns via metadata and hands over to the repository ({@see \App\Core\Traits\Api\Models\Repositories\TreeTrait::moveSubtree()}).
- moveTreeNode() : Result
- reorderTreeChildren() : Result
- Reorders the direct children of a parent into the given order (whole subtrees); resolves columns and hands over to the repository ({@see \App\Core\Traits\Api\Models\Repositories\TreeTrait::reorderTreeChildren()}).
- resolveTreeColumn() : string|null
- Maps a property to a database column via metadata (scalar field or to-one association join column). Returns null when the property does not exist.
- resolveTreeStructure() : TreeStructure|Result
- Resolves a `TreeConfig` to table, columns and scope columns via Doctrine metadata.
Methods
closeTreeGap()
Closes the gap in `lft`/`rgt` left by a deleted subtree.
public
closeTreeGap(int $lft, int $rgt, TreeConfig $config) : Result
Parameters
- $lft : int
- $rgt : int
- $config : TreeConfig
Return values
ResultgetTreeSubtree()
Returns ids of the whole subtree of a node plus its `lft`/`rgt` bounds (the basis for deleting the subtree and closing the gap afterwards).
public
getTreeSubtree(int $id, TreeConfig $config) : Result
Parameters
- $id : int
- $config : TreeConfig
Return values
ResultinsertTreeNode()
Computes `lft`/`rgt`/`level` of a newly inserted node (after it is saved with `parent_id`); resolves columns via metadata and hands over to the repository ({@see \App\Core\Traits\Api\Models\Repositories\TreeTrait::insertTreeNode()}).
public
insertTreeNode(int $id, TreeConfig $config) : Result
Parameters
- $id : int
- $config : TreeConfig
Return values
ResultinTreeLock()
Runs the operation under the tree lock of the given `TreeConfig` (table plus scope).
public
inTreeLock(TreeConfig $config, callable(): Result $operation) : Result
Parameters
- $config : TreeConfig
- $operation : callable(): Result
Tags
Return values
ResultmoveSubtree()
Moves a whole subtree under another parent; resolves columns via metadata and hands over to the repository ({@see \App\Core\Traits\Api\Models\Repositories\TreeTrait::moveSubtree()}).
public
moveSubtree(int $id, int|null $newParentId, TreeConfig $config[, int|null $afterSiblingId = null ][, TreeMoveExpectation|null $expectation = null ]) : Result
Parameters
- $id : int
- $newParentId : int|null
-
null moves to the root level
- $config : TreeConfig
- $afterSiblingId : int|null = null
-
null = last child, 0 = first child, >0 = after that sibling
- $expectation : TreeMoveExpectation|null = null
-
client contract verified under the lock, null skips the check
Return values
ResultmoveTreeNode()
public
moveTreeNode(int $id, int $direction, TreeConfig $config) : Result
Parameters
- $id : int
- $direction : int
- $config : TreeConfig
Return values
ResultreorderTreeChildren()
Reorders the direct children of a parent into the given order (whole subtrees); resolves columns and hands over to the repository ({@see \App\Core\Traits\Api\Models\Repositories\TreeTrait::reorderTreeChildren()}).
public
reorderTreeChildren(int|null $parentId, array<int, int> $orderedIds, TreeConfig $config) : Result
Parameters
- $parentId : int|null
-
null is the root level
- $orderedIds : array<int, int>
-
child ids in the new order
- $config : TreeConfig
Return values
ResultresolveTreeColumn()
Maps a property to a database column via metadata (scalar field or to-one association join column). Returns null when the property does not exist.
private
resolveTreeColumn(ClassMetadata<string|int, object> $meta, string $property) : string|null
Parameters
- $meta : ClassMetadata<string|int, object>
- $property : string
Return values
string|nullresolveTreeStructure()
Resolves a `TreeConfig` to table, columns and scope columns via Doctrine metadata.
private
resolveTreeStructure(TreeConfig $config) : TreeStructure|Result
Returns a failed Result when a property cannot be mapped to a column.
Parameters
- $config : TreeConfig