TreeBehavior Class Reference

Inheritance diagram for TreeBehavior:

ModelBehavior Object

List of all members.


Public Member Functions

 _setParent (&$model, $parentId=null, $created=false)
 afterSave (&$model, $created)
 beforeDelete (&$model)
 beforeSave (&$model)
 childcount (&$model, $id=null, $direct=false)
 children (&$model, $id=null, $direct=false, $fields=null, $order=null, $limit=null, $page=1, $recursive=null)
 generatetreelist (&$model, $conditions=null, $keyPath=null, $valuePath=null, $spacer= '_', $recursive=null)
 getparentnode (&$model, $id=null, $fields=null, $recursive=null)
 getpath (&$model, $id=null, $fields=null, $recursive=null)
 movedown (&$model, $id=null, $number=1)
 moveup (&$model, $id=null, $number=1)
 recover (&$model, $mode= 'parent', $missingParentAction=null)
 removefromtree (&$model, $id=null, $delete=false)
 reorder (&$model, $options=array())
 setparent (&$model, $parentId=null, $created=null)
 setScope (&$model, $scope)
 setup (&$model, $config=array())
 verify (&$model)

Public Attributes

 $_defaults
 $errors = array()

Detailed Description

Definition at line 37 of file tree.php.


Member Function Documentation

TreeBehavior::_setParent ( &$  model,
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
mixed $parentId
Returns:
boolean true on success, false on failure protected

Definition at line 809 of file tree.php.

TreeBehavior::afterSave ( &$  model,
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
boolean $created indicates whether the node just saved was created or updated
Returns:
boolean true on success, false on failure

Reimplemented from ModelBehavior.

Definition at line 80 of file tree.php.

TreeBehavior::beforeDelete ( &$  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
Returns:
boolean true to continue, false to abort the delete

Definition at line 99 of file tree.php.

TreeBehavior::beforeSave ( &$  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.

Since:
1.2
Parameters:
AppModel $model
Returns:
boolean true to continue, false to abort the save

Reimplemented from ModelBehavior.

Definition at line 130 of file tree.php.

TreeBehavior::childcount ( &$  model,
id = null,
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
mixed $id The ID of the record to read or false to read all top level nodes
boolean $direct whether to count direct, or all, children
Returns:
integer number of child nodes public

Definition at line 203 of file tree.php.

TreeBehavior::children ( &$  model,
id = null,
direct = false,
fields = null,
order = null,
limit = null,
page = 1,
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
mixed $id The ID of the record to read
boolean $direct whether to return only the direct, or all, children
mixed $fields Either a single string of a field name, or an array of field names
string $order SQL ORDER BY conditions (e.g. "price DESC" or "name ASC") defaults to the tree order
integer $limit SQL LIMIT clause, for calculating items per page.
integer $page Page number, for accessing paged data
integer $recursive The number of levels deep to fetch associated records
Returns:
array Array of child nodes public

Definition at line 244 of file tree.php.

References order.

TreeBehavior::generatetreelist ( &$  model,
conditions = null,
keyPath = null,
valuePath = null,
spacer = '_',
recursive = null 
)

A convenience method for returning a hierarchical array used for HTML select boxes

Parameters:
AppModel $model
mixed $conditions SQL conditions as a string or as an array('field' =>'value',...)
string $keyPath A string path to the key, i.e. "{n}.Post.id"
string $valuePath A string path to the value, i.e. "{n}.Post.title"
string $spacer The character or characters which will be repeated
integer $recursive The number of levels deep to fetch associated records
Returns:
array An associative array of records, where the id is the key, and the display field is the value public

Definition at line 300 of file tree.php.

References Set::combine(), and order.

TreeBehavior::getparentnode ( &$  model,
id = null,
fields = null,
recursive = null 
)

Get the parent node

reads the parent id and returns this node

Parameters:
AppModel $model
mixed $id The ID of the record to read
integer $recursive The number of levels deep to fetch associated records
Returns:
array Array of data for the parent node public

Definition at line 354 of file tree.php.

TreeBehavior::getpath ( &$  model,
id = null,
fields = null,
recursive = null 
)

Get the path to the given node

Parameters:
AppModel $model
mixed $id The ID of the record to read
mixed $fields Either a single string of a field name, or an array of field names
integer $recursive The number of levels deep to fetch associated records
Returns:
array Array of nodes from top most parent to current node public

Definition at line 387 of file tree.php.

References order.

TreeBehavior::movedown ( &$  model,
id = null,
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
mixed $id The ID of the record to move
mixed $number how many places to move the node or true to move to last position
Returns:
boolean true on success, false on failure public

Definition at line 423 of file tree.php.

TreeBehavior::moveup ( &$  model,
id = null,
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
mixed $id The ID of the record to move
mixed $number how many places to move the node, or true to move to first position
Returns:
boolean true on success, false on failure public

Definition at line 480 of file tree.php.

TreeBehavior::recover ( &$  model,
mode = 'parent',
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.

Todo:
Could be written to be faster, *maybe*. Ideally using a subquery and putting all the logic burden on the DB.
Parameters:
AppModel $model
string $mode parent or tree
mixed $missingParentAction 'return' to do nothing and return, 'delete' to delete, or the id of the parent to set as the parent_id
Returns:
boolean true on success, false on failure public

Definition at line 543 of file tree.php.

References order.

TreeBehavior::removefromtree ( &$  model,
id = null,
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
mixed $id The ID of the record to remove
boolean $delete whether to delete the node after reparenting children (if any)
Returns:
boolean true on success, false on failure public

Definition at line 648 of file tree.php.

TreeBehavior::reorder ( &$  model,
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.

Parameters:
AppModel $model
array $options
Returns:
boolean true on success, false on failure

Definition at line 613 of file tree.php.

References order.

TreeBehavior::setparent ( &$  model,
parentId = null,
created = null 
)

Backward compatible method

Returns true if the change is successful.

Parameters:
AppModel $model
mixed $parentId The ID to set as the parent of the current node.
Returns:
true on success public
Deprecated:

Definition at line 714 of file tree.php.

References a().

TreeBehavior::setScope ( &$  model,
scope 
)

Deprecated:

Definition at line 63 of file tree.php.

References BehaviorCollection::attach().

TreeBehavior::setup ( &$  model,
config = array() 
)

Setup this behavior with the specified configuration settings.

Parameters:
object $model Model using this behavior
array $config Configuration settings for $model public

Reimplemented from ModelBehavior.

Definition at line 46 of file tree.php.

References $config, and ModelBehavior::$settings.

TreeBehavior::verify ( &$  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
Returns:
mixed true if the tree is valid or empty, otherwise an array of (error type [index, node], [incorrect left/right index,node id], message) public

Definition at line 736 of file tree.php.

References a().


Member Data Documentation

TreeBehavior::$_defaults

Initial value:

 array(
        'parent' => 'parent_id', 'left' => 'lft', 'right' => 'rght',
        'scope' => '1 = 1', 'type' => 'nested', '__parentChange' => false, 'recursive' => -1
    )

Definition at line 41 of file tree.php.

TreeBehavior::$errors = array()

Definition at line 39 of file tree.php.


The documentation for this class was generated from the following file: