Tree Behavior.
Enables a model object to act as a node-based tree. Using Modified Preorder Tree Traversal
_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 integer
_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 integer
_getNode( Model $Model , integer|string $id )
Returns a single node from the tree from its primary key
Model
$Model
$id
array|boolean
_getOptions( array $arg )
Convenience method to create default find() options from $arg when it is an associative array.
$arg
array
_recoverByParentId( Model $Model , integer $counter 1 , mixed $parentId null )
_recoverByParentId
Recursive helper function used by recover
Model
$Model
$counter
optional 1 $parentId
optional null integer
_setChildrenLevel( Model $Model , integer|string $id )
Set level for descendents.
Model
$Model
$id
_setParent( Model $Model , integer|string $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 boolean
_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
boolean
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() boolean
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 boolean
ModelBehavior::beforeDelete()
beforeFind( Model $Model , array $query )
Runs before a find() operation
Model
$Model
$query
array
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() boolean
Model::save()
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 integer
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 array
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() 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 array
getLevel( Model $Model , integer|string $id null )
Returns the depth level of a node in the tree.
Model
$Model
$id
optional null integer|boolean
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 array|boolean
getPath( Model $Model , integer|string $id null , string|array $fields null , integer $recursive null )
Get the path to the given node
Model
$Model
$id
optional null $fields
optional null $recursive
optional null array
moveDown( Model $Model , integer|string $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 boolean
moveUp( Model $Model , integer|string $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 boolean
recover( Model $Model , string $mode 'parent' , string|integer $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 boolean
removeFromTree( Model $Model , integer|string $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 boolean
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() boolean
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
mixed
_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 mixed
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
mixed
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() mixed
Model::save()
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
BehaviorCollection::detach()
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() mixed
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 boolean
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
$extra
optional array() mixed
toString( )
Object-to-string conversion. Each class can override this method as necessary.
string
public array
Allows the mapping of preg-compatible regular expressions to public or private methods in this class, where the array key is a /-delimited regular expression, and the value is a class method. Similar to the functionality of the findBy* / findAllBy* magic methods.
array()
public array
Contains configuration settings for use with individual model objects. This is used because if multiple models use this Behavior, each will use the same object instance. Individual model settings should be stored as an associative array, keyed off of the model name.
Model::$alias
array()
© 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.
http://api.cakephp.org/2.7/class-TreeBehavior.html