Jump to content

Community

Developer Documentation

Parent/Child relationships with Nodes

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

public static $databaseColumnParent = 'string';

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.

 

public static $nodeSortable = boolean;

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.

protected $lockParents = boolean;

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.

 

protected $protectRoots = boolean;

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.

public mixed parent()

Returns the immediate parent node, or NULL if this is a root item with no parent.

 

public \SplStack parents()

Returns a stack of all parents (the immediate parent node, that node's parent, etc. up to the root node).

 

public boolean hasChildren( [string|null $permissionCheck='view' [, \IPS\Member|null $member=NULL [, boolean $subnodes=TRUE [, mixed $where=array() ]]]] )

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

 

public int childrenCount( [string|null $permissionCheck='view' [, \IPS\Member|null $member=NULL [, boolean $subnodes=TRUE [, mixed $where=array() ]]]] )

Returns the number of children to this node. Accepts the same parameters as hasChildren, above.

 

public array children( [string|null $permissionCheck='view' [, \IPS\Member|null $member=NULL [, boolean $subnodes=TRUE [, array|null $skip=NULL [, mixed $where=array() ]]]]] )

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

 

public boolean isChildOf( \IPS\Node\Model $node )

Returns true or false indicating whether this node is a child (at any depth) of the provided $node.


  Report Document


×
×
  • Create New...