In most situations, nodes will allow other nodes to be children, thus forming a parent-child relationship with each other. An example of this is forums, where a forum can contain sub-forums, which can contain further sub-forums, and so on. Each forum is a node, and they exist as a tree structure, with parent and child forums.
To support parent/child relationships, your node model simply needs to define a parent property (see below). Doing so will make a number of inherited methods available to your class. Additionally, administrators will be able to reorder and change the parents of nodes in the AdminCP (although this behavior can be configured).
Configuring your model class
Simply define this property in your class to implement parent/child relationships. The value should be the name of the database column (without prefix) which contains the ID number of the node parent.
If false (default is true), then nodes will not be sortable by administrators in the AdminCP.
Configuring your controller class
By default, your node controllers do not need any properties or methods added to support parent/child relationships. However, there are some properties you can define to control their behaviors.
If true, nodes will not be able to be moved out of their parents. They'll only be able to be reordered within their existing parent.
If true, root nodes will not be able to become child nodes, and child nodes will not be able to become root nodes.
Supported methods
After implementing parent/child relationships in your model, the following methods will be available.
Returns the immediate parent node, or NULL if this is a root item with no parent.
Returns a stack of all parents (the immediate parent node, that node's parent, etc. up to the root node).
Returns true of false indicating whether this node has child nodes.
-
$permissionCheck
The permission key to check to make a node count (pass null to not check permissions) -
$member
The member to use as the context when checking permissions (pass null to use the currently-logged in member) -
$subnodes
Whether to only count immediate children. If false, all children of the node will be counted, regardless of depth. -
$where
Additional where clauses to pass to the query
Returns the number of children to this node. Accepts the same parameters as hasChildren, above.
Returns an array of child nodes.
-
$permissionCheck
The permission key to check to make a node count (pass null to not check permissions) -
$member
The member to use as the context when checking permissions (pass null to use the currently-logged in member) -
$subnodes
Whether to only count immediate children. If false, all children of the node will be counted, regardless of depth. -
$skip
An array of child IDs to skip -
$where
Additional where clauses to pass to the query
Returns true or false indicating whether this node is a child (at any depth) of the provided $node.
Report Document