Tree Behavior.
Enables a model object to act as a node-based tree. Using Modified Preorder Tree Traversal
$_defaults
protected array
$_deletedRow
protected array
$errors
public array
Convenience method to create default find() options from $arg when it is an associative array.
_getMax( Model $Model , string $scope , string $right , integer $recursive -1 , boolean $created false )
get the maximum index value in the table.
Model
$Model
$scope
$right
$recursive
optional -1 $created
optional false _getMin( Model $Model , string $scope , string $left , integer $recursive -1 )
get the minimum index value in the table.
Model
$Model
$scope
$left
$recursive
optional -1 _getNode( Model $Model , integer|string $id )
Returns a single node from the tree from its primary key
Model
$Model
$id
_getOptions( array $arg )
Convenience method to create default find() options from $arg when it is an associative array.
$arg
_recoverByParentId( Model $Model , integer $counter 1 , integer|string|null $parentId null )
_recoverByParentId
Recursive helper function used by recover
Model
$Model
$counter
optional 1 $parentId
optional null _setChildrenLevel( Model $Model , integer|string $id )
Set level for descendents.
Model
$Model
$id
_setParent( Model $Model , integer|string|null $parentId null , boolean $created false )
Sets the parent of the given node
The force parameter is used to override the "don't change the parent to the current parent" logic in the event of recovering a corrupted table, or creating new nodes. Otherwise it should always be false. In reality this method could be private, since calling save with parent_id set also calls setParent
Model
$Model
$parentId
optional null $created
optional false _sync( Model $Model , integer $shift , string $dir '+' , array $conditions array() , boolean $created false , string $field 'both' )
Table sync method.
Handles table sync operations, Taking account of the behavior scope.
Model
$Model
$shift
$dir
optional '+' $conditions
optional array() $created
optional false $field
optional 'both' afterDelete( Model $Model )
After delete method.
Will delete the current node and all children using the deleteAll method and sync the table
Model
$Model
ModelBehavior::afterDelete()
afterSave( Model $Model , boolean $created , array $options array() )
After save method. Called after all saves
Overridden to transparently manage setting the lft and rght fields if and only if the parent field is included in the parameters to be saved.
Model
$Model
$created
$options
optional array() ModelBehavior::afterSave()
beforeDelete( Model $Model , boolean $cascade true )
Stores the record about to be deleted.
This is used to delete child nodes in the afterDelete.
Model
$Model
$cascade
optional true ModelBehavior::beforeDelete()
beforeFind( Model $Model , array $query )
Runs before a find() operation
Model
$Model
$query
ModelBehavior::beforeFind()
beforeSave( Model $Model , array $options array() )
Before save method. Called before all saves
Overridden to transparently manage setting the lft and rght fields if and only if the parent field is included in the parameters to be saved. For newly created nodes with NO parent the left and right field values are set directly by this method bypassing the setParent logic.
Model
$Model
$options
optional array() ModelBehavior::beforeSave()
childCount( Model $Model , integer|string|boolean $id null , boolean $direct false )
Get the number of child nodes
If the direct parameter is set to true, only the direct children are counted (based upon the parent_id field) If false is passed for the id parameter, all top level nodes are counted, or all nodes are counted.
Model
$Model
$id
optional null $direct
optional false children( Model $Model , integer|string $id null , boolean $direct false , string|array $fields null , string $order null , integer $limit null , integer $page 1 , integer $recursive null )
Get the child nodes of the current model
If the direct parameter is set to true, only the direct children are returned (based upon the parent_id field) If false is passed for the id parameter, top level, or all (depending on direct parameter appropriate) are counted.
Model
$Model
$id
optional null $direct
optional false $fields
optional null $order
optional null $limit
optional null $page
optional 1 $recursive
optional null formatTreeList( Model $Model , array $results , array $options array() )
Formats result of a find() call to a hierarchical array used for HTML select boxes.
Note that when using your own find() call this expects the order to be "left" field asc in order to generate the same result as using generateTreeList() directly.
Options:
Model
$Model
$results
$options
optional array() generateTreeList( Model $Model , string|array $conditions null , string $keyPath null , string $valuePath null , string $spacer '_' , integer $recursive null )
A convenience method for returning a hierarchical array used for HTML select boxes
Model
$Model
$conditions
optional null $keyPath
optional null $valuePath
optional null $spacer
optional '_' $recursive
optional null getLevel( Model $Model , integer|string|null $id null )
Returns the depth level of a node in the tree.
Model
$Model
$id
optional null getParentNode( Model $Model , integer|string $id null , string|array $fields null , integer $recursive null )
Get the parent node
reads the parent id and returns this node
Model
$Model
$id
optional null $fields
optional null $recursive
optional null getPath( Model $Model , integer|string|null $id null , string|array|null $fields null , integer|null $recursive null )
Get the path to the given node
Model
$Model
$id
optional null $fields
optional null $recursive
optional null moveDown( Model $Model , integer|string|null $id null , integer|boolean $number 1 )
Reorder 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
Model
$Model
$id
optional null $number
optional 1 moveUp( Model $Model , integer|string|null $id null , integer|boolean $number 1 )
Reorder the node without changing the parent.
If the node is the first child, or is a top level node with no previous node this method will return false
Model
$Model
$id
optional null $number
optional 1 recover( Model $Model , string $mode 'parent' , string|integer|null $missingParentAction null )
Recover a corrupted tree
The mode parameter is used to specify the source of info that is valid/correct. The opposite source of data will be populated based upon that source of info. E.g. if the MPTT fields are corrupt or empty, with the $mode 'parent' the values of the parent_id field will be used to populate the left and right fields. The missingParentAction parameter only applies to "parent" mode and determines what to do if the parent field contains an id that is not present.
Model
$Model
$mode
optional 'parent' $missingParentAction
optional null 'return' to do nothing and return, 'delete' to delete, or the id of the parent to set as the parent_id
removeFromTree( Model $Model , integer|string|null $id null , boolean $delete false )
Remove the current node from the tree, and reparent all children up one level.
If the parameter delete is false, the node will become a new top level node. Otherwise the node will be deleted after the children are reparented.
Model
$Model
$id
optional null $delete
optional false reorder( Model $Model , array $options array() )
Reorder method.
Reorders the nodes (and child nodes) of the tree according to the field and direction specified in the parameters. This method does not change the parent of any node.
Requires a valid tree, by default it verifies the tree before beginning.
Options:
Model
$Model
$options
optional array() setup( Model $Model , array $config array() )
Initiate Tree behavior
Model
$Model
$config
optional array() ModelBehavior::setup()
verify( Model $Model )
Check if the current tree is valid.
Returns true if the tree is valid otherwise an array of (type, incorrect left/right index, message)
Model
$Model
true if the tree is valid or empty, otherwise an array of (error type [index, node], [incorrect left/right index,node id], message)
_addToWhitelist( Model $model , string $field )
If $model's whitelist property is non-empty, $field will be added to it. Note: this method should only be used in beforeValidate or beforeSave to ensure that it only modifies the whitelist for the current save operation. Also make sure you explicitly set the value of the field which you are allowing.
Model
$model
$field
afterFind( Model $model , mixed $results , boolean $primary false )
After find callback. Can be used to modify any results returned by find.
Model
$model
$results
$primary
optional false afterValidate( Model $model )
afterValidate is called just after model data was validated, you can use this callback to perform any data cleanup or preparation if needed
Model
$model
beforeValidate( Model $model , array $options array() )
beforeValidate is called before a model is validated, you can use this callback to add behavior validation rules into a models validate array. Returning false will allow you to make the validation fail.
Model
$model
$options
optional array() cleanup( Model $model )
Clean up any initialization this behavior has done on a model. Called when a behavior is dynamically detached from a model using Model::detach().
Model
$model
onError( Model $model , string $error )
DataSource error callback
Model
$model
$error
_mergeVars( array $properties , string $class , boolean $normalize true )
Merges this objects $property with the property in $class' definition. This classes value for the property will be merged on top of $class'
This provides some of the DRY magic CakePHP provides. If you want to shut it off, redefine this method as an empty function.
$properties
$class
$normalize
optional true _set( array $properties array() )
Allows setting of multiple properties of the object in a single line of code. Will only set properties that are part of a class declaration.
$properties
optional array() _stop( integer|string $status 0 )
Stop execution of the current script. Wraps exit() making testing easier.
$status
optional 0 dispatchMethod( string $method , array $params array() )
Calls a method on this object with the given parameters. Provides an OO wrapper for call_user_func_array
$method
$params
optional array() log( string $msg , integer $type LOG_ERR , null|string|array $scope null )
Convenience method to write a message to CakeLog. See CakeLog::write() for more information on writing to logs.
$msg
$type
optional LOG_ERR $scope
optional null The scope(s) a log message is being created in. See CakeLog::config() for more information on logging scopes.
requestAction( string|array $url , array $extra array() )
Calls a controller's method from any location. Can be used to connect controllers together or tie plugins into a main application. requestAction can be used to return rendered views or fetch the return value from controller actions.
Under the hood this method uses Router::reverse() to convert the $url parameter into a string URL. You should use URL formats that are compatible with Router::reverse()
POST and GET data can be simulated in requestAction. Use $extra['url']
for GET data. The $extra['data']
parameter allows POST data simulation.
$url
String or array-based URL. Unlike other URL arrays in CakePHP, this URL will not automatically handle passed and named arguments in the $url parameter.
$extra
optional array() if array includes the key "return" it sets the AutoRender to true. Can also be used to submit GET/POST data, and named/passed arguments.
Boolean true or false on success/failure, or contents of rendered action if 'return' is set in $extra.
toString( )
CakeObject-to-string conversion. Each class can override this method as necessary.
protected array
Defaults
array( 'parent' => 'parent_id', 'left' => 'lft', 'right' => 'rght', 'level' => null, 'scope' => '1 = 1', 'type' => 'nested', '__parentChange' => false, 'recursive' => -1 )
© 2005–2016 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/2.9/class-TreeBehavior.html