Class TreeBehavior
Tree Behavior.
Enables a model object to act as a node-based tree. Using Modified Preorder Tree Traversal
- Object
- ModelBehavior
- TreeBehavior
See: http://en.wikipedia.org/wiki/Tree_traversal
Link: http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html
Copyright: Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
License: MIT License
Location: Cake/Model/Behavior/TreeBehavior.php
Properties summary
-
$_defaults
protectedarray
Defaults -
$_deletedRow
protectedarray
Used to preserve state between delete callbacks. -
$errors
publicarray
Errors
Inherited Properties
Method Summary
-
_getMax() protected
get the maximum index value in the table. -
_getMin() protected
get the minimum index value in the table. -
_setParent() protected
Sets the parent of the given node -
_sync() protected
Table sync method. -
afterDelete() public
After delete method. -
afterSave() public
After save method. Called after all saves -
beforeDelete() public
Stores the record about to be deleted. -
beforeFind() public
Runs before a find() operation -
beforeSave() public
Before save method. Called before all saves -
childCount() public
Get the number of child nodes -
children() public
Get the child nodes of the current model -
generateTreeList() public
A convenience method for returning a hierarchical array used for HTML select boxes -
getParentNode() public
Get the parent node -
getPath() public
Get the path to the given node -
moveDown() public
Reorder the node without changing the parent. -
moveUp() public
Reorder the node without changing the parent. -
recover() public
Recover a corrupted tree -
removeFromTree() public
Remove the current node from the tree, and reparent all children up one level. -
reorder() public
Reorder method. -
setup() public
Initiate Tree behavior -
verify() public
Check if the current tree is valid.
Method Detail
_getMax() protected ¶
_getMax( Model
$Model , string $scope , string $right , integer $recursive = -1 , boolean $created = false )
get the maximum index value in the table.
Parameters
-
Model
$Model - string $scope
- string $right
- integer $recursive optional -1
- boolean $created optional false
Returns
_getMin() protected ¶
_getMin( Model
$Model , string $scope , string $left , integer $recursive = -1 )
get the minimum index value in the table.
Parameters
-
Model
$Model - string $scope
- string $left
- integer $recursive optional -1
Returns
_setParent() protected ¶
_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
Parameters
-
Model
$Model - Model instance
- integer|string $parentId optional null
- boolean $created optional false
Returns
true on success, false on failure
_sync() protected ¶
_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.
Parameters
-
Model
$Model - integer $shift
- string $dir optional '+'
- array $conditions optional array()
- boolean $created optional false
- string $field optional 'both'
afterDelete() public ¶
afterDelete( Model
$Model )
After delete method.
Will delete the current node and all children using the deleteAll method and sync the table
Parameters
-
Model
$Model - Model instance
Returns
true to continue, false to abort the delete
Overrides
afterSave() public ¶
afterSave( Model
$Model , boolean $created )
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.
Parameters
-
Model
$Model - Model instance.
- boolean $created
- indicates whether the node just saved was created or updated
Returns
true on success, false on failure
Overrides
beforeDelete() public ¶
beforeDelete( Model
$Model , boolean $cascade = true )
Stores the record about to be deleted.
This is used to delete child nodes in the afterDelete.
Parameters
-
Model
$Model - Model instance
- boolean $cascade optional true
Returns
Overrides
beforeFind() public ¶
beforeFind( Model
$Model , array $query )
Runs before a find() operation
Parameters
-
Model
$Model - Model using the behavior
- array $query
- Query parameters as set by cake
Returns
Overrides
beforeSave() public ¶
beforeSave( Model
$Model )
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.
Parameters
-
Model
$Model - Model instance
Returns
true to continue, false to abort the save
Since
Overrides
childCount() public ¶
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.
Parameters
-
Model
$Model - Model instance
- integer|string|boolean $id optional null
- The ID of the record to read or false to read all top level nodes
- boolean $direct optional false
- whether to count direct, or all, children
Returns
number of child nodes
Link
children() public ¶
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.
Parameters
-
Model
$Model - Model instance
- integer|string $id optional null
- The ID of the record to read
- boolean $direct optional false
- whether to return only the direct, or all, children
- string|array $fields optional null
- Either a single string of a field name, or an array of field names
- string $order optional null
- SQL ORDER BY conditions (e.g. "price DESC" or "name ASC") defaults to the tree order
- integer $limit optional null
- SQL LIMIT clause, for calculating items per page.
- integer $page optional 1
- Page number, for accessing paged data
- integer $recursive optional null
- The number of levels deep to fetch associated records
Returns
Array of child nodes
Link
generateTreeList() public ¶
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
Parameters
-
Model
$Model - Model instance
- string|array $conditions optional null
- SQL conditions as a string or as an array('field' =>'value',...)
- string $keyPath optional null
- A string path to the key, i.e. "{n}.Post.id"
- string $valuePath optional null
- A string path to the value, i.e. "{n}.Post.title"
- string $spacer optional '_'
- The character or characters which will be repeated
- integer $recursive optional null
- The number of levels deep to fetch associated records
Returns
An associative array of records, where the id is the key, and the display field is the value
Link
getParentNode() public ¶
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
Parameters
-
Model
$Model - Model instance
- integer|string $id optional null
- The ID of the record to read
- string|array $fields optional null
- integer $recursive optional null
- The number of levels deep to fetch associated records
Returns
Array of data for the parent node
Link
getPath() public ¶
getPath( Model
$Model , integer|string $id = null , string|array $fields = null , integer $recursive = null )
Get the path to the given node
Parameters
-
Model
$Model - Model instance
- integer|string $id optional null
- The ID of the record to read
- string|array $fields optional null
- Either a single string of a field name, or an array of field names
- integer $recursive optional null
- The number of levels deep to fetch associated records
Returns
Array of nodes from top most parent to current node
Link
moveDown() public ¶
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
Parameters
-
Model
$Model - Model instance
- integer|string $id optional null
- The ID of the record to move
- integer|boolean $number optional 1
- how many places to move the node or true to move to last position
Returns
true on success, false on failure
Link
moveUp() public ¶
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
Parameters
-
Model
$Model - Model instance
- integer|string $id optional null
- The ID of the record to move
- integer|boolean $number optional 1
- how many places to move the node, or true to move to first position
Returns
true on success, false on failure
Link
recover() public ¶
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.
Parameters
-
Model
$Model - Model instance
- string $mode optional 'parent'
- parent or tree
- string|integer $missingParentAction optional null
'return' to do nothing and return, 'delete' to delete, or the id of the parent to set as the parent_id
Returns
true on success, false on failure
Link
removeFromTree() public ¶
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.
Parameters
-
Model
$Model - Model instance
- integer|string $id optional null
- The ID of the record to remove
- boolean $delete optional false
- whether to delete the node after reparenting children (if any)
Returns
true on success, false on failure
Link
reorder() public ¶
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:
- 'id' id of record to use as top node for reordering
- 'field' Which field to use in reordering defaults to displayField
- 'order' Direction to order either DESC or ASC (defaults to ASC)
- 'verify' Whether or not to verify the tree before reorder. defaults to true.
Parameters
-
Model
$Model - Model instance
- array $options optional array()
- array of options to use in reordering.
Returns
true on success, false on failure
Link
setup() public ¶
setup( Model
$Model , array $config = array() )
Initiate Tree behavior
Parameters
-
Model
$Model - instance of model
- array $config optional array()
- array of configuration settings.
Overrides
verify() public ¶
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)
Parameters
-
Model
$Model - Model instance
Returns
true if the tree is valid or empty, otherwise an array of (error type [index, node], [incorrect left/right index,node id], message)
Link
Methods inherited from ModelBehavior
_addToWhitelist() protected ¶
_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.
Parameters
-
Model
$model - Model using this behavior
- string $field
- Field to be added to $model's whitelist
afterFind() public ¶
afterFind( Model
$model , mixed $results , boolean $primary )
After find callback. Can be used to modify any results returned by find.
Parameters
-
Model
$model - Model using this behavior
- mixed $results
- The results of the find operation
- boolean $primary
- Whether this model is being queried directly (vs. being queried as an association)
Returns
An array value will replace the value of $results - any other value will be ignored.
afterValidate() public ¶
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
Parameters
-
Model
$model - Model using this behavior
Returns
False will stop this event from being passed to other behaviors
beforeValidate() public ¶
beforeValidate( Model
$model )
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.
Parameters
-
Model
$model - Model using this behavior
Returns
False or null will abort the operation. Any other result will continue.
Methods inherited from Object
_mergeVars() protected ¶
_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.
Parameters
- array $properties
- The name of the properties to merge.
- string $class
- The class to merge the property with.
- boolean $normalize optional true
- Set to true to run the properties through Hash::normalize() before merging.
_set() protected ¶
_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.
Parameters
- array $properties optional array()
- An associative array containing properties and corresponding values.
_stop() protected ¶
_stop( integer|string $status = 0 )
Stop execution of the current script. Wraps exit() making testing easier.
Parameters
- integer|string $status optional 0
- see http://php.net/exit for values
dispatchMethod() public ¶
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
Parameters
- string $method
- Name of the method to call
- array $params optional array()
- Parameter list to use when calling $method
Returns
Returns the result of the method call
log() public ¶
log( string $msg , integer|string $type = LOG_ERR )
Convenience method to write a message to CakeLog. See CakeLog::write() for more information on writing to logs.
Parameters
- string $msg
- Log message.
- integer|string $type optional LOG_ERR
Type of message being written. Either a valid LOG_* constant or a string matching the recognized levels.
Returns
Success of log write.
See
requestAction() public ¶
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()
Passing POST and GET data
POST and GET data can be simulated in requestAction. Use $extra['url']
for
GET data. The $extra['data']
parameter allows POST data simulation.
Parameters
- string|array $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.
- array $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.
Returns
Boolean true or false on success/failure, or contents of rendered action if 'return' is set in $extra.
toString() public ¶
toString( )
Object-to-string conversion. Each class can override this method as necessary.
Returns
The name of this class
Properties detail
$_defaults ¶
Defaults
array( 'parent' => 'parent_id', 'left' => 'lft', 'right' => 'rght', 'scope' => '1 = 1', 'type' => 'nested', '__parentChange' => false, 'recursive' => -1 )