TreeBehavior Class Reference

Inheritance diagram for TreeBehavior:

ModelBehavior Object List of all members.

Public Member Functions

 __getMax ($Model, $scope, $right, $recursive=-1, $created=false)
 __getMin ($Model, $scope, $left, $recursive=-1)
 __sync (&$Model, $shift, $dir= '+', $conditions=array(), $created=false, $field= 'both')
 _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())
 setup (&$Model, $config=array())
 verify (&$Model)

Public Attributes

 $_defaults
 $errors = array()

Detailed Description

Definition at line 36 of file tree.php.


Member Function Documentation

TreeBehavior::__getMax ( Model,
scope,
right,
recursive = -1,
created = false 
)

get the maximum index value in the table.

Parameters:
AppModel $Model
string $scope
string $right
Returns:
int private

Definition at line 870 of file tree.php.

References ConnectionManager::getDataSource().

TreeBehavior::__getMin ( Model,
scope,
left,
recursive = -1 
)

get the minimum index value in the table.

Parameters:
AppModel $Model
string $scope
string $right
Returns:
int private

Definition at line 897 of file tree.php.

References ConnectionManager::getDataSource().

TreeBehavior::__sync ( &$  Model,
shift,
dir = '+',
conditions = array(),
created = false,
field = 'both' 
)

Table sync method.

Handles table sync operations, Taking account of the behavior scope.

Parameters:
AppModel $Model
integer $shift
string $direction
array $conditions
string $field private

Definition at line 919 of file tree.php.

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 Model instance
mixed $parentId
Returns:
boolean true on success, false on failure protected

Definition at line 804 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 Model instance.
boolean $created indicates whether the node just saved was created or updated
Returns:
boolean true on success, false on failure public

Reimplemented from ModelBehavior.

Definition at line 86 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 Model instance
Returns:
boolean true to continue, false to abort the delete public

Definition at line 106 of file tree.php.

References read().

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 Model instance
Returns:
boolean true to continue, false to abort the save public

Reimplemented from ModelBehavior.

Definition at line 138 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 Model instance
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 211 of file tree.php.

References $id.

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 Model instance
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 256 of file tree.php.

References $id, and 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 Model instance
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 312 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 Model instance
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 366 of file tree.php.

References $id.

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

Get the path to the given node

Parameters:
AppModel $Model Model instance
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 398 of file tree.php.

References $id, and 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 Model instance
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 434 of file tree.php.

References $id.

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 Model instance
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 491 of file tree.php.

References $id.

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 Model instance
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 554 of file tree.php.

References $id, $path, ConnectionManager::getDataSource(), and 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 Model instance
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 666 of file tree.php.

References $id, and ConnectionManager::getDataSource().

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.

Options:

Parameters:
AppModel $Model Model instance
array $options array of options to use in reordering.
Returns:
boolean true on success, false on failure

Definition at line 631 of file tree.php.

References $id, and order.

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

Initiate Tree behavior

Parameters:
object $Model instance of model
array $config array of configuration settings.
Returns:
void public

Reimplemented from ModelBehavior.

Definition at line 61 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 Model instance
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 733 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 49 of file tree.php.

TreeBehavior::$errors = array()

Definition at line 42 of file tree.php.


The documentation for this class was generated from the following file:
Generated on Sun Nov 22 00:30:57 2009 for CakePHP 1.2.x.x (v1.2.4.8284) by doxygen 1.4.7