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 http://www.sitepoint.com/hierarchical-data-database-2/
$_defaultConfig
protected array
$_primaryKey
protected string
Ensures that the provided entity contains non-empty values for the left and right fields
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
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.
Before save listener. Transparently manages setting the lft and rght fields if the parent field is included in the parameters to be saved.
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.
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.
_ensureFields( Cake\Datasource\EntityInterface $entity )
Ensures that the provided entity contains non-empty values for the left and right fields
Cake\Datasource\EntityInterface
$entity
_getNode( mixed $id )
Returns a single node from the tree from its primary key
$id
Cake\ORM\Entity
Cake\Datasource\Exception\RecordNotFoundException
_getPrimaryKey( )
Returns a single string value representing the primary key of the attached table
_moveDown( Cake\Datasource\EntityInterface $node , integer|boolean $number )
Helper function used with the actual code for moveDown
Cake\Datasource\EntityInterface
$node
$number
Cake\ORM\Entity
|booleanCake\Datasource\Exception\RecordNotFoundException
_moveUp( Cake\Datasource\EntityInterface $node , integer|boolean $number )
Helper function used with the actual code for moveUp
Cake\Datasource\EntityInterface
$node
$number
Cake\ORM\Entity
|booleanCake\Datasource\Exception\RecordNotFoundException
_recoverTree( integer $counter 0 , mixed $parentId null , integer $level -1 )
Recursive method used to recover a single level of the tree
$counter
optional 0 $parentId
optional null $level
optional -1 _removeFromTree( Cake\Datasource\EntityInterface $node )
Helper function containing the actual code for removeFromTree
Cake\Datasource\EntityInterface
$node
Cake\ORM\Entity
|falsethe node after being removed from the tree or false on error
_scope( Cake\ORM\Query $query )
Alters the passed query so that it only returns scoped records as defined in the tree configuration.
Cake\ORM\Query
$query
Cake\ORM\Query
_setAsRoot( Cake\Datasource\EntityInterface $entity )
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
_setChildrenLevel( Cake\Datasource\EntityInterface $entity )
Set level for descendants.
Cake\Datasource\EntityInterface
$entity
_setParent( Cake\Datasource\EntityInterface $entity , mixed $parent )
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
$parent
_sync( integer $shift , string $dir , string $conditions , boolean $mark false )
Auxiliary function used to automatically alter the value of both the left and right columns by a certain amount that match the passed conditions
$shift
$dir
$conditions
a SQL snipped to be used for comparing left or right against it.
$mark
optional false whether to mark the updated values so that they can not be modified by future calls to this function.
_unmarkInternalTree( )
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.
afterSave( Cake\Event\Event $event , Cake\Datasource\EntityInterface $entity )
After save listener.
Manages updating level of descendants of currently saved entity.
Cake\Event\Event
$event
Cake\Datasource\EntityInterface
$entity
beforeDelete( Cake\Event\Event $event , Cake\Datasource\EntityInterface $entity )
Also deletes the nodes in the subtree of the entity to be delete
Cake\Event\Event
$event
Cake\Datasource\EntityInterface
$entity
beforeSave( Cake\Event\Event $event , Cake\Datasource\EntityInterface $entity )
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\Event
$event
Cake\Datasource\EntityInterface
$entity
childCount( Cake\Datasource\EntityInterface $node , boolean $direct false )
Get the number of children nodes.
Cake\Datasource\EntityInterface
$node
$direct
optional false whether to count all nodes in the subtree or just direct children
findChildren( Cake\ORM\Query $query , array $options )
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
$options
Cake\ORM\Query
findPath( Cake\ORM\Query $query , array $options )
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
$options
Cake\ORM\Query
findTreeList( Cake\ORM\Query $query , array $options )
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
$options
Cake\ORM\Query
formatTreeList( Cake\ORM\Query $query , array $options [] )
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
$options
optional [] Cake\ORM\Query
getLevel( integer|string|Cake\Datasource\EntityInterface $entity )
Returns the depth level of a node in the tree.
Cake\Datasource\EntityInterface
$entity
initialize( array $config )
Constructor hook method.
$config
Cake\ORM\Behavior::initialize()
moveDown( Cake\Datasource\EntityInterface $node , integer|boolean $number 1 )
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 false
Cake\Datasource\EntityInterface
$node
$number
optional 1 Cake\ORM\Entity
|booleanCake\Datasource\Exception\RecordNotFoundException
moveUp( Cake\Datasource\EntityInterface $node , integer|boolean $number 1 )
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 false
Cake\Datasource\EntityInterface
$node
$number
optional 1 Cake\ORM\Entity
|booleanCake\Datasource\Exception\RecordNotFoundException
recover( )
Recovers the lft and right column values out of the hierarchy defined by the parent column.
removeFromTree( Cake\Datasource\EntityInterface $node )
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
Cake\ORM\Entity
|falsethe node after being removed from the tree or false on error
__construct( Cake\ORM\Table $table , array $config [] )
Constructor
Merges config with the default and store in the config property
Cake\ORM\Table
$table
$config
optional [] _reflectionCache( )
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
_resolveMethodAliases( string $key , array $defaults , array $config )
Removes aliased methods that would otherwise be duplicated by userland configuration.
$key
$defaults
$config
getTable( )
Get the table instance this behavior is bound to.
Cake\ORM\Table
implementedEvents( )
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.
Cake\Event\EventListenerInterface::implementedEvents()
implementedFinders( )
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
implementedMethods( )
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
verifyConfig( )
verifyConfig
Checks that implemented keys contain values pointing at callable.
Cake\Core\Exception\Exception
_configDelete( string $key )
Deletes a single config key.
$key
Cake\Core\Exception\Exception
_configRead( string|null $key )
Reads a config key.
$key
_configWrite( string|array $key , mixed $value , boolean|string $merge false )
Writes a config key.
$key
$value
$merge
optional false True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
Cake\Core\Exception\Exception
config( string|array|null $key null , mixed|null $value null , boolean $merge true )
Gets/Sets the config.
Reading the whole config:
$this->config();
Reading a specific value:
$this->config('key');
Reading a nested value:
$this->config('some.nested.key');
Setting a specific value:
$this->config('key', $value);
Setting a nested value:
$this->config('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->config(['one' => 'value', 'another' => 'value']);
$key
optional null $value
optional null $merge
optional true Cake\Core\Exception\Exception
configShallow( string|array $key , mixed|null $value null )
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->config('key', $value);
Setting a nested value:
$this->config('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->config(['one' => 'value', 'another' => 'value']);
$key
$value
optional null getConfig( string|null $key null )
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');
$key
optional null setConfig( string|array $key , mixed|null $value null , boolean $merge true )
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']);
$key
$value
optional null $merge
optional true Cake\Core\Exception\Exception
protected array
Default config
These are merged with user-provided configuration when the behavior is used.
[ 'implementedFinders' => [ 'path' => 'findPath', 'children' => 'findChildren', 'treeList' => 'findTreeList', ], 'implementedMethods' => [ 'childCount' => 'childCount', 'moveUp' => 'moveUp', 'moveDown' => 'moveDown', 'recover' => 'recover', 'removeFromTree' => 'removeFromTree', 'getLevel' => 'getLevel', 'formatTreeList' => 'formatTreeList', ], 'parent' => 'parent_id', 'left' => 'lft', 'right' => 'rght', 'scope' => null, 'level' => null, 'recoverOrder' => null, ]
© 2005–2017 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/3.4/class-Cake.ORM.Behavior.TreeBehavior.html