class Nos\Orm_Behaviour_Tree
Makes a Nos\Orm\Model behaves like a Tree.
An item can then have a parent and children (all of the same Model).


property Nos\Orm_Behaviour_Tree::$parent_relation

Required. Name of the parent relation.

property Nos\Orm_Behaviour_Tree::$children_relation

Required. Name of the children relation.

property Nos\Orm_Behaviour_Tree::$level_property

Column used to store the item’s depth inside the tree. Data type must be int.


Returns:Model parent item, if it exists, null otherwise.

Set a new parent for the item.

If the item is twinnable and if it exists in several contexts, all contexts will be moved synchronously.

  • $new_parent (Model) – New parent Model of the item (use null to remove the parent).

Exception when:

  • the item is moved in its own tree ;
  • the item is twinnable and its parent does not exist in one of the contexts of the current item.
Nos\Orm_Behaviour_Tree::find_children($where = array(), $order_by = array(), $options = array())
Returns:All direct children of item.

Children can be filter and / or sort by parameters.

This method use native method Model->find().
$options parameter are passed to ->find() like that:
$options = \Arr::merge($options, array(
        'where'    => $where,
        'order_by' => $order_by,
Nos\Orm_Behaviour_Tree::find_children_recursive($include_self = true)
  • $include_self (boolean) – If true, include current item in return.

All children of item and their descendants.

Returns:First ascendant of item in tree or null if item has no parent.


This behaviour extends Model->find().

Add option to where array passed to method : you can use parent key as alias for search in Orm_Behaviour_Tree::$parent_relation relation.


class Model_Page extends \Nos\Orm\Model
        protected static $_behaviours = array(
                'Nos\Orm_Behaviour_Tree' => array(
                        'events' => array('before_query', 'after_delete'),
                        'parent_relation' => 'parent',
                        'children_relation' => 'children',
                        'level_property' => 'page_level',

        protected static $_has_many = array(
                'children' => array(
                        'key_from'       => 'page_id',
                        'model_to'       => 'Nos\Model_Page',
                        'key_to'         => 'page_parent_id',
                        'cascade_save'   => false,
                        'cascade_delete' => false,

        protected static $_belongs_to = array(
                'parent' => array(
                        'key_from'       => 'page_parent_id',
                        'model_to'       => 'Nos\Model_Page',
                        'key_to'         => 'page_id',
                        'cascade_save'   => false,
                        'cascade_delete' => false,