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
Copyright: Copyright 2005-2012, Cake Software Foundation, Inc.
License: The MIT License
Location: model/behaviors/tree.php
Properties summary
-
$_defaults
publicarray
Defaults -
$errors
publicarray
Errors
Inherited Properties
Method Summary
-
__getMax() public
get the maximum index value in the table. -
__getMin() public
get the minimum index value in the table. -
__sync() public
Table sync method. -
_setParent() public
Sets the parent of the given node -
afterSave() public
After save method. Called after all saves -
beforeDelete() public
Before delete method. Called before all deletes -
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() public ¶
__getMax( AppModel
$Model , string $scope , string $right , $recursive = -1 , $created = false )
get the maximum index value in the table.
Parameters
-
AppModel
$Model - string $scope
- string $right
- $recursive optional -1
- $created optional false
Returns
__getMin() public ¶
__getMin( AppModel
$Model , string $scope , string $left , $recursive = -1 )
get the minimum index value in the table.
Parameters
-
AppModel
$Model - string $scope
- string $left
- $right
- $recursive optional -1
Returns
__sync() public ¶
__sync( AppModel
$Model , integer $shift , string $dir = '+' , array $conditions = array() , string $created = false , $field = 'both' )
Table sync method.
Handles table sync operations, Taking account of the behavior scope.
Parameters
-
AppModel
$Model - integer $shift
- string $dir optional '+'
- $direction
- array $conditions optional array()
- string $created optional false
- $field
- $field optional 'both'
_setParent() public ¶
_setParent( AppModel
$Model , mixed $parentId = null , $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
-
AppModel
$Model - Model instance
- mixed $parentId optional null
- $created optional false
Returns
true on success, false on failure
afterSave() public ¶
afterSave( AppModel
$Model , boolean $created )
After save method. Called after all saves
Overriden 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
-
AppModel
$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( AppModel
$Model )
Before delete method. Called before all deletes
Will delete the current node and all children using the deleteAll method and sync the table
Parameters
-
AppModel
$Model - Model instance
Returns
true to continue, false to abort the delete
Overrides
beforeSave() public ¶
beforeSave( AppModel
$Model )
Before save method. Called before all saves
Overriden 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
-
AppModel
$Model - Model instance
Returns
true to continue, false to abort the save
Since
Overrides
childcount() public ¶
childcount( AppModel
$Model , mixed $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
-
AppModel
$Model - Model instance
- mixed $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
children() public ¶
children( AppModel
$Model , mixed $id = null , boolean $direct = false , mixed $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
-
AppModel
$Model - Model instance
- mixed $id optional null
- The ID of the record to read
- boolean $direct optional false
- whether to return only the direct, or all, children
- mixed $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
generatetreelist() public ¶
generatetreelist( AppModel
$Model , mixed $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
-
AppModel
$Model - Model instance
- mixed $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
getparentnode() public ¶
getparentnode( AppModel
$Model , mixed $id = null , integer $fields = null , $recursive = null )
Get the parent node
reads the parent id and returns this node
Parameters
-
AppModel
$Model - Model instance
- mixed $id optional null
- The ID of the record to read
- integer $fields optional null
- $recursive The number of levels deep to fetch associated records
- $recursive optional null
Returns
Array of data for the parent node
getpath() public ¶
getpath( AppModel
$Model , mixed $id = null , mixed $fields = null , integer $recursive = null )
Get the path to the given node
Parameters
-
AppModel
$Model - Model instance
- mixed $id optional null
- The ID of the record to read
- mixed $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
movedown() public ¶
movedown( AppModel
$Model , mixed $id = null , mixed $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
-
AppModel
$Model - Model instance
- mixed $id optional null
- The ID of the record to move
- mixed $number optional 1
- how many places to move the node or true to move to last position
Returns
true on success, false on failure
moveup() public ¶
moveup( AppModel
$Model , mixed $id = null , mixed $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
-
AppModel
$Model - Model instance
- mixed $id optional null
- The ID of the record to move
- mixed $number optional 1
- how many places to move the node, or true to move to first position
Returns
true on success, false on failure
recover() public ¶
recover( AppModel
$Model , string $mode = 'parent' , mixed $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
-
AppModel
$Model - Model instance
- string $mode optional 'parent'
- parent or tree
- mixed $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
removefromtree() public ¶
removefromtree( AppModel
$Model , mixed $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
-
AppModel
$Model - Model instance
- mixed $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
reorder() public ¶
reorder( AppModel
$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 reordeing 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
-
AppModel
$Model - Model instance
- array $options optional array()
- array of options to use in reordering.
Returns
true on success, false on failure
setup() public ¶
setup( object $Model , array $config = array() )
Initiate Tree behavior
Parameters
- object $Model
- instance of model
- array $config optional array()
- array of configuration settings.
Overrides
verify() public ¶
verify( AppModel
$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
-
AppModel
$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)
Methods inherited from ModelBehavior
_addToWhitelist() public ¶
_addToWhitelist( object $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
- object $model
- Model using this behavior
- string $field
- Field to be added to $model's whitelist
afterDelete() public ¶
afterDelete( object $model )
After delete callback
Parameters
- object $model
- Model using this behavior
afterFind() public ¶
afterFind( object $model , mixed $results , boolean $primary )
After find callback. Can be used to modify any results returned by find and findAll.
Parameters
- object $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
Result of the find operation
beforeFind() public ¶
beforeFind( object $model , array $query )
Before find callback
Parameters
- object $model
- Model using this behavior
- array $query
- $queryData Data used to execute this query, i.e. conditions, order, etc.
Returns
True if the operation should continue, false if it should abort
beforeValidate() public ¶
beforeValidate( object $model )
Before validate callback
Parameters
- object $model
- Model using this behavior
Returns
True if validate operation should continue, false to abort
cleanup() public ¶
cleanup( object $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().
Parameters
- object $model
- Model using this behavior
See
dispatchMethod() public ¶
dispatchMethod( string $model , array $method , $params = array() )
Overrides Object::dispatchMethod to account for PHP4's broken reference support
Parameters
- string $model
- $method Name of the method to call
- array $method
- $params Parameter list to use when calling $method
- $params optional array()
Returns
See
Overrides
Methods inherited from Object
Object() public ¶
Object( )
A hack to support __construct() on PHP 4 Hint: descendant classes have no PHP4 class_name() constructors, so this constructor gets called first and calls the top-layer __construct() which (if present) should call parent::__construct()
Returns
__openPersistent() public ¶
__openPersistent( string $name , string $type = null )
Open the persistent class file for reading Used by Object::_persist()
Parameters
- string $name
- Name of persisted class
- string $type optional null
- Type of persistance (e.g: registry)
_persist() public ¶
_persist( string $name , string $return , $object , $type = null )
Checks for a persistent class file, if found file is opened and true returned If file is not found a file is created and false returned If used in other locations of the model you should choose a unique name for the persistent file There are many uses for this method, see manual for examples
Parameters
- string $name
- name of the class to persist
- string $return
- $object the object to persist
- $object
- $type optional null
Returns
Success
_savePersistent() public ¶
_savePersistent( string $name , object $object )
You should choose a unique name for the persistent file
There are many uses for this method, see manual for examples
Parameters
- string $name
- name used for object to cache
- object $object
- the object to persist
Returns
true on save, throws error if file can not be created
_set() public ¶
_set( array $properties = array() )
Allows setting of multiple properties of the object in a single line of code.
Parameters
- array $properties optional array()
- An associative array containing properties and corresponding values.
_stop() public ¶
_stop( $status = 0 )
Stop execution of the current script
Parameters
- $status optional 0
- http://php.net/exit for values
cakeError() public ¶
cakeError( string $method , array $messages = array() )
Used to report user friendly errors. If there is a file app/error.php or app/app_error.php this file will be loaded error.php is the AppError class it should extend ErrorHandler class.
Parameters
- string $method
- Method to be called in the error class (AppError or ErrorHandler classes)
- array $messages optional array()
- Message that is to be displayed by the error class
Returns
message
log() public ¶
log( string $msg , integer $type = LOG_ERROR )
API for logging events.
Parameters
- string $msg
- Log message
- integer $type optional LOG_ERROR
- Error type constant. Defined in app/config/core.php.
Returns
Success of log write
requestAction() public ¶
requestAction( mixed $url , array $extra = array() )
Calls a controller's method from any location.
Parameters
- mixed $url
- String or array-based url.
- array $extra optional array()
- if array includes the key "return" it sets the AutoRender to true.
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 )