Makes the table to which this is attached to behave like a nested set and provides methods for managing and retrieving information out of the derived hierarchical structure.
Tables attaching this behavior are required to have a column referencing the parent row, and two other numeric columns (lft and rght) where the implicit order will be cached.
For more information on what is a nested set and a how it works refer to https://www.sitepoint.com/hierarchical-data-database-2/
array<string, mixed>Runtime config
boolWhether the config property has already been configured with defaults
array<string, mixed>Default config
stringCached copy of the first column in a table's primary key.
array<string, array>Reflection method cache for behaviors.
Cake\ORM\TableTable instance.
Constructor
Deletes a single config key.
Reads a config key.
Writes a config key.
Ensures that the provided entity contains non-empty values for the left and right fields
Returns the maximum index value in the table.
Returns a single node from the tree from its primary key
Returns a single string value representing the primary key of the attached table
Helper function used with the actual code for moveDown
Helper function used with the actual code for moveUp
Recursive method used to recover a single level of the tree
Gets the methods implemented by this behavior
Helper function containing the actual code for removeFromTree
Removes aliased methods that would otherwise be duplicated by userland configuration.
Alters the passed query so that it only returns scoped records as defined in the tree configuration.
Updates the left and right column for the passed entity so it can be set as a new root in the tree. It also modifies the ordering in the rest of the tree so the structure remains valid
Set level for descendants.
Sets the correct left and right values for the passed entity so it can be updated to a new parent. It also makes the hole in the tree so the node move can be done without corrupting the structure.
Auxiliary function used to automatically alter the value of both the left and right columns by a certain amount that match the passed conditions
Helper method used to invert the sign of the left and right columns that are less than 0. They were set to negative values before so their absolute value wouldn't change while performing other tree transformations.
After save listener.
Also deletes the nodes in the subtree of the entity to be delete
Before save listener. Transparently manages setting the lft and rght fields if the parent field is included in the parameters to be saved.
Get the number of children nodes.
Merge provided config with existing config. Unlike config() which does a recursive merge for nested keys, this method does a simple merge.
Get the children nodes of the current model
Custom finder method which can be used to return the list of nodes from the root to a specific node in the tree. This custom finder requires that the key 'for' is passed in the options containing the id of the node to get its path for.
Gets a representation of the elements in the tree as a flat list where the keys are the primary key for the table and the values are the display field for the table. Values are prefixed to visually indicate relative depth in the tree.
Formats query as a flat list where the keys are the primary key for the table and the values are the display field for the table. Values are prefixed to visually indicate relative depth in the tree.
Returns the config.
Returns the config for this specific key.
Returns the depth level of a node in the tree.
Get the table instance this behavior is bound to.
Gets the Model callbacks this behavior is interested in.
implementedFinders
implementedMethods
Constructor hook method.
Reorders the node without changing the parent.
Reorders the node without changing its parent.
Recovers the lft and right column values out of the hierarchy defined by the parent column.
Removes the current node from the tree, by positioning it as a new root and re-parents all children up one level.
Sets the config.
Get the table instance this behavior is bound to.
verifyConfig
__construct(Cake\ORM\Table $table, array<string, mixed> $config = [])
Constructor
Merges config with the default and store in the config property
Cake\ORM\Table $table The table this behavior is attached to.
array<string, mixed> $config optional The config for this behavior.
_configDelete(string $key): void
Deletes a single config key.
string $key Key to delete.
voidCake\Core\Exception\CakeException_configRead(string|null $key): mixed
Reads a config key.
string|null $key Key to read.
mixed_configWrite(array<string, mixed>|string $key, mixed $value, string|bool $merge = false): void
Writes a config key.
array<string, mixed>|string $key Key to write to.
mixed $value Value to write.
string|bool $merge optional True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
voidCake\Core\Exception\CakeException_ensureFields(Cake\Datasource\EntityInterface $entity): void
Ensures that the provided entity contains non-empty values for the left and right fields
Cake\Datasource\EntityInterface $entity The entity to ensure fields for
void_getMax(): int
Returns the maximum index value in the table.
int_getNode(mixed $id): Cake\Datasource\EntityInterface
Returns a single node from the tree from its primary key
mixed $id Record id.
Cake\Datasource\EntityInterfaceCake\Datasource\Exception\RecordNotFoundException_getPrimaryKey(): string
Returns a single string value representing the primary key of the attached table
string_moveDown(Cake\Datasource\EntityInterface $node, int|true $number): Cake\Datasource\EntityInterface
Helper function used with the actual code for moveDown
Cake\Datasource\EntityInterface $node The node to move
int|true $number How many places to move the node, or true to move to last position
Cake\Datasource\EntityInterfaceCake\Datasource\Exception\RecordNotFoundException_moveUp(Cake\Datasource\EntityInterface $node, int|true $number): Cake\Datasource\EntityInterface
Helper function used with the actual code for moveUp
Cake\Datasource\EntityInterface $node The node to move
int|true $number How many places to move the node, or true to move to first position
Cake\Datasource\EntityInterfaceCake\Datasource\Exception\RecordNotFoundException_recoverTree(int $lftRght = 1, mixed $parentId = null, int $level = 0): int
Recursive method used to recover a single level of the tree
int $lftRght optional The starting lft/rght value
mixed $parentId optional the parent id of the level to be recovered
int $level optional Node level
int_reflectionCache(): array
Gets the methods implemented by this behavior
Uses the implementedEvents() method to exclude callback methods. Methods starting with _ will be ignored, as will methods declared on Cake\ORM\Behavior
arrayReflectionException_removeFromTree(Cake\Datasource\EntityInterface $node): Cake\Datasource\EntityInterface|false
Helper function containing the actual code for removeFromTree
Cake\Datasource\EntityInterface $node The node to remove from the tree
Cake\Datasource\EntityInterface|false_resolveMethodAliases(string $key, array<string, mixed> $defaults, array<string, mixed> $config): array
Removes aliased methods that would otherwise be duplicated by userland configuration.
string $key The key to filter.
array<string, mixed> $defaults The default method mappings.
array<string, mixed> $config The customized method mappings.
array_scope(Cake\ORM\Query $query): Cake\ORM\Query
Alters the passed query so that it only returns scoped records as defined in the tree configuration.
Cake\ORM\Query $query the Query to modify
Cake\ORM\Query_setAsRoot(Cake\Datasource\EntityInterface $entity): void
Updates the left and right column for the passed entity so it can be set as a new root in the tree. It also modifies the ordering in the rest of the tree so the structure remains valid
Cake\Datasource\EntityInterface $entity The entity to set as a new root
void_setChildrenLevel(Cake\Datasource\EntityInterface $entity): void
Set level for descendants.
Cake\Datasource\EntityInterface $entity The entity whose descendants need to be updated.
void_setParent(Cake\Datasource\EntityInterface $entity, mixed $parent): void
Sets the correct left and right values for the passed entity so it can be updated to a new parent. It also makes the hole in the tree so the node move can be done without corrupting the structure.
Cake\Datasource\EntityInterface $entity The entity to re-parent
mixed $parent the id of the parent to set
voidRuntimeException_sync(int $shift, string $dir, string $conditions, bool $mark = false): void
Auxiliary function used to automatically alter the value of both the left and right columns by a certain amount that match the passed conditions
int $shift the value to use for operating the left and right columns
string $dir The operator to use for shifting the value (+/-)
string $conditions a SQL snipped to be used for comparing left or right against it.
bool $mark optional whether to mark the updated values so that they can not be modified by future calls to this function.
void_unmarkInternalTree(): void
Helper method used to invert the sign of the left and right columns that are less than 0. They were set to negative values before so their absolute value wouldn't change while performing other tree transformations.
voidafterSave(Cake\Event\EventInterface $event, Cake\Datasource\EntityInterface $entity): void
After save listener.
Manages updating level of descendants of currently saved entity.
Cake\Event\EventInterface $event The afterSave event that was fired
Cake\Datasource\EntityInterface $entity the entity that is going to be saved
voidbeforeDelete(Cake\Event\EventInterface $event, Cake\Datasource\EntityInterface $entity): void
Also deletes the nodes in the subtree of the entity to be delete
Cake\Event\EventInterface $event The beforeDelete event that was fired
Cake\Datasource\EntityInterface $entity The entity that is going to be saved
voidbeforeSave(Cake\Event\EventInterface $event, Cake\Datasource\EntityInterface $entity): void
Before save listener. Transparently manages setting the lft and rght fields if the parent field is included in the parameters to be saved.
Cake\Event\EventInterface $event The beforeSave event that was fired
Cake\Datasource\EntityInterface $entity the entity that is going to be saved
voidRuntimeExceptionchildCount(Cake\Datasource\EntityInterface $node, bool $direct = false): int
Get the number of children nodes.
Cake\Datasource\EntityInterface $node The entity to count children for
bool $direct optional whether to count all nodes in the subtree or just direct children
intconfigShallow(array<string, mixed>|string $key, mixed|null $value = null): $this
Merge provided config with existing config. Unlike config() which does a recursive merge for nested keys, this method does a simple merge.
Setting a specific value:
$this->configShallow('key', $value); Setting a nested value:
$this->configShallow('some.nested.key', $value); Updating multiple config settings at the same time:
$this->configShallow(['one' => 'value', 'another' => 'value']);
array<string, mixed>|string $key The key to set, or a complete array of configs.
mixed|null $value optional The value to set.
$thisfindChildren(Cake\ORM\Query $query, array<string, mixed> $options): Cake\ORM\Query
Get the children nodes of the current model
Available options are:
If the direct option is set to true, only the direct children are returned (based upon the parent_id field)
Cake\ORM\Query $query Query.
array<string, mixed> $options Array of options as described above
Cake\ORM\QueryInvalidArgumentExceptionfindPath(Cake\ORM\Query $query, array<string, mixed> $options): Cake\ORM\Query
Custom finder method which can be used to return the list of nodes from the root to a specific node in the tree. This custom finder requires that the key 'for' is passed in the options containing the id of the node to get its path for.
Cake\ORM\Query $query The constructed query to modify
array<string, mixed> $options the list of options for the query
Cake\ORM\QueryInvalidArgumentExceptionfindTreeList(Cake\ORM\Query $query, array<string, mixed> $options): Cake\ORM\Query
Gets a representation of the elements in the tree as a flat list where the keys are the primary key for the table and the values are the display field for the table. Values are prefixed to visually indicate relative depth in the tree.
Cake\ORM\Query $query Query.
array<string, mixed> $options Array of options as described above.
Cake\ORM\QueryformatTreeList(Cake\ORM\Query $query, array<string, mixed> $options = []): Cake\ORM\Query
Formats query as a flat list where the keys are the primary key for the table and the values are the display field for the table. Values are prefixed to visually indicate relative depth in the tree.
Cake\ORM\Query $query The query object to format.
array<string, mixed> $options optional Array of options as described above.
Cake\ORM\QuerygetConfig(string|null $key = null, mixed $default = null): mixed
Returns the config.
Reading the whole config:
$this->getConfig();
Reading a specific value:
$this->getConfig('key'); Reading a nested value:
$this->getConfig('some.nested.key'); Reading with default value:
$this->getConfig('some-key', 'default-value'); string|null $key optional The key to get or null for the whole config.
mixed $default optional The return value when the key does not exist.
mixedgetConfigOrFail(string $key): mixed
Returns the config for this specific key.
The config value for this key must exist, it can never be null.
string $key The key to get.
mixedInvalidArgumentExceptiongetLevel(Cake\Datasource\EntityInterface|string|int $entity): int|false
Returns the depth level of a node in the tree.
Cake\Datasource\EntityInterface|string|int $entity The entity or primary key get the level of.
int|falsegetTable(): Cake\ORM\Table
Get the table instance this behavior is bound to.
Cake\ORM\TableimplementedEvents(): array<string, mixed>
Gets the Model callbacks this behavior is interested in.
By defining one of the callback methods a behavior is assumed to be interested in the related event.
Override this method if you need to add non-conventional event listeners. Or if you want your behavior to listen to non-standard events.
array<string, mixed>implementedFinders(): array
implementedFinders
Provides an alias->methodname map of which finders a behavior implements. Example:
[ 'this' => 'findThis', 'alias' => 'findMethodName' ]
With the above example, a call to $table->find('this') will call $behavior->findThis() and a call to $table->find('alias') will call $behavior->findMethodName()
It is recommended, though not required, to define implementedFinders in the config property of child classes such that it is not necessary to use reflections to derive the available method list. See core behaviors for examples
arrayReflectionExceptionimplementedMethods(): array
implementedMethods
Provides an alias->methodname map of which methods a behavior implements. Example:
[ 'method' => 'method', 'aliasedMethod' => 'somethingElse' ]
With the above example, a call to $table->method() will call $behavior->method() and a call to $table->aliasedMethod() will call $behavior->somethingElse()
It is recommended, though not required, to define implementedFinders in the config property of child classes such that it is not necessary to use reflections to derive the available method list. See core behaviors for examples
arrayReflectionExceptioninitialize(array<string, mixed> $config): void
Constructor hook method.
Implement this method to avoid having to overwrite the constructor and call parent.
array<string, mixed> $config voidmoveDown(Cake\Datasource\EntityInterface $node, int|true $number = 1): Cake\Datasource\EntityInterface|false
Reorders the node without changing the parent.
If the node is the last child, or is a top level node with no subsequent node this method will return the same node without any changes
Cake\Datasource\EntityInterface $node The node to move
int|true $number optional How many places to move the node or true to move to last position
Cake\Datasource\EntityInterface|falseCake\Datasource\Exception\RecordNotFoundExceptionmoveUp(Cake\Datasource\EntityInterface $node, int|true $number = 1): Cake\Datasource\EntityInterface|false
Reorders the node without changing its parent.
If the node is the first child, or is a top level node with no previous node this method will return the same node without any changes
Cake\Datasource\EntityInterface $node The node to move
int|true $number optional How many places to move the node, or true to move to first position
Cake\Datasource\EntityInterface|falseCake\Datasource\Exception\RecordNotFoundExceptionrecover(): void
Recovers the lft and right column values out of the hierarchy defined by the parent column.
voidremoveFromTree(Cake\Datasource\EntityInterface $node): Cake\Datasource\EntityInterface|false
Removes the current node from the tree, by positioning it as a new root and re-parents all children up one level.
Note that the node will not be deleted just moved away from its current position without moving its children with it.
Cake\Datasource\EntityInterface $node The node to remove from the tree
Cake\Datasource\EntityInterface|falsesetConfig(array<string, mixed>|string $key, mixed|null $value = null, bool $merge = true): $this
Sets the config.
Setting a specific value:
$this->setConfig('key', $value); Setting a nested value:
$this->setConfig('some.nested.key', $value); Updating multiple config settings at the same time:
$this->setConfig(['one' => 'value', 'another' => 'value']);
array<string, mixed>|string $key The key to set, or a complete array of configs.
mixed|null $value optional The value to set.
bool $merge optional Whether to recursively merge or overwrite existing config, defaults to true.
$thisCake\Core\Exception\CakeExceptiontable(): Cake\ORM\Table
Get the table instance this behavior is bound to.
Cake\ORM\TableverifyConfig(): void
verifyConfig
Checks that implemented keys contain values pointing at callable.
voidCake\Core\Exception\CakeExceptionRuntime config
array<string, mixed>Whether the config property has already been configured with defaults
boolDefault config
These are merged with user-provided configuration when the behavior is used.
array<string, mixed>Cached copy of the first column in a table's primary key.
stringReflection method cache for behaviors.
Stores the reflected method + finder methods per class. This prevents reflecting the same class multiple times in a single process.
array<string, array>Table instance.
Cake\ORM\Table
© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/4.4/class-Cake.ORM.Behavior.TreeBehavior.html