PHP Класс Tree\NestedTree\NestedTree

Показать файл Открыть проект

Открытые методы

Метод Описание
__construct ( string $table, string $idField, string $parentField, string $sortField ) Constructor. Set the database table name and necessary field names
generateTreeData ( &$arr, integer $id, integer $level, &$n ) Generate the tree data. A single call to this generates the n-values for 1 node in the tree. This function assigns the passed in n value as the node's nleft value. It then processes all the node's children (which in turn recursively processes that node's children and so on), and when it is finally done, it takes the update n-value and assigns it as its nright value. Because it is passed as a reference, the subsequent changes in subrequests are held over to when control is returned so the nright can be assigned.
getChildren ( integer $id, boolean $includeSelf = false ) : array Fetch the children of a node, or if no node is specified, fetch the top level items.
getDescendants ( integer $id, boolean $includeSelf = false, boolean $childrenOnly = false, $unique_id_list = false ) : array Fetch the descendants of a node, or if no node is specified, fetch the entire tree. Optionally, only return child data instead of all descendant data.
getFields ( ) : array A utility function to return an array of the fields that need to be selected in SQL select queries
getImmediateFamily ( integer $id ) : array Fetch the immediately family of a node. More specifically, fetch a node's parent, siblings and children. If the node isn't valid, fetch the first level of nodes from the tree.
getNode ( integer $id ) : object Fetch the node data for the node identified by $id
getPath ( integer $id, boolean $includeSelf = false ) : array Fetch the path to a node. If an invalid node is passed, an empty array is returned.
getTreeWithChildren ( ) : array Fetch the tree data, nesting within each node references to the node's children
isChildOf ( integer $child_id, integer $parent_id ) : boolean Check if one node is a child of another node. If either node is not found, then false is returned.
isDescendantOf ( integer $descendant_id, integer $ancestor_id ) : boolean Check if one node descends from another node. If either node is not found, then false is returned.
numChildren ( integer $id ) : integer Find the number of children a node has
numDescendants ( integer $id ) : integer Find the number of descendants a node has
rebuild ( ) Rebuilds the tree data and saves it to the database

Описание методов

__construct() публичный Метод

Constructor. Set the database table name and necessary field names
public __construct ( string $table, string $idField, string $parentField, string $sortField )
$table string Name of the tree database table
$idField string Name of the primary key ID field
$parentField string Name of the parent ID field
$sortField string Name of the field to sort data.

generateTreeData() публичный Метод

Generate the tree data. A single call to this generates the n-values for 1 node in the tree. This function assigns the passed in n value as the node's nleft value. It then processes all the node's children (which in turn recursively processes that node's children and so on), and when it is finally done, it takes the update n-value and assigns it as its nright value. Because it is passed as a reference, the subsequent changes in subrequests are held over to when control is returned so the nright can be assigned.
public generateTreeData ( &$arr, integer $id, integer $level, &$n )
$id integer The ID of the current node to process
$level integer The nlevel to assign to the current node

getChildren() публичный Метод

Fetch the children of a node, or if no node is specified, fetch the top level items.
public getChildren ( integer $id, boolean $includeSelf = false ) : array
$id integer The ID of the node to fetch child data for.
$includeSelf boolean Whether or not to include the passed node in the the results.
Результат array The children of the passed node

getDescendants() публичный Метод

Fetch the descendants of a node, or if no node is specified, fetch the entire tree. Optionally, only return child data instead of all descendant data.
public getDescendants ( integer $id, boolean $includeSelf = false, boolean $childrenOnly = false, $unique_id_list = false ) : array
$id integer The ID of the node to fetch descendant data for. Specify an invalid ID (e.g. 0) to retrieve all data.
$includeSelf boolean Whether or not to include the passed node in the the results. This has no meaning if fetching entire tree.
$childrenOnly boolean True if only returning children data. False if returning all descendant data
Результат array The descendants of the passed now

getFields() публичный Метод

A utility function to return an array of the fields that need to be selected in SQL select queries
public getFields ( ) : array
Результат array An indexed array of fields to select

getImmediateFamily() публичный Метод

Fetch the immediately family of a node. More specifically, fetch a node's parent, siblings and children. If the node isn't valid, fetch the first level of nodes from the tree.
public getImmediateFamily ( integer $id ) : array
$id integer The ID of the node to fetch child data for.
Результат array An array of each node in the family

getNode() публичный Метод

Fetch the node data for the node identified by $id
public getNode ( integer $id ) : object
$id integer The ID of the node to fetch
Результат object An object containing the node's data, or null if node not found

getPath() публичный Метод

If a top level node is passed, an array containing on that node is included (if 'includeSelf' is set to true, otherwise an empty array)
public getPath ( integer $id, boolean $includeSelf = false ) : array
$id integer The ID of the node to fetch child data for.
$includeSelf boolean Whether or not to include the passed node in the the results.
Результат array An array of each node to passed node

getTreeWithChildren() публичный Метод

Fetch the tree data, nesting within each node references to the node's children
public getTreeWithChildren ( ) : array
Результат array The tree with the node's child data

isChildOf() публичный Метод

Check if one node is a child of another node. If either node is not found, then false is returned.
public isChildOf ( integer $child_id, integer $parent_id ) : boolean
$child_id integer The node that is possibly a child
$parent_id integer The node that is possibly a parent
Результат boolean True if $child_id is a child of $parent_id, false otherwise

isDescendantOf() публичный Метод

Check if one node descends from another node. If either node is not found, then false is returned.
public isDescendantOf ( integer $descendant_id, integer $ancestor_id ) : boolean
$descendant_id integer The node that potentially descends
$ancestor_id integer The node that is potentially descended from
Результат boolean True if $descendant_id descends from $ancestor_id, false otherwise

numChildren() публичный Метод

Find the number of children a node has
public numChildren ( integer $id ) : integer
$id integer The ID of the node to search for. Pass 0 to count the first level items
Результат integer The number of descendants the node has, or -1 if the node isn't found.

numDescendants() публичный Метод

Find the number of descendants a node has
public numDescendants ( integer $id ) : integer
$id integer The ID of the node to search for. Pass 0 to count all nodes in the tree.
Результат integer The number of descendants the node has, or -1 if the node isn't found.

rebuild() публичный Метод

Rebuilds the tree data and saves it to the database
public rebuild ( )