1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
3 * Object Relational Mapping (ORM) "tree" extension. Allows ORM objects to act
4 * as trees, with parents and children.
6 * $Id: ORM_Tree.php 3917 2009-01-21 03:06:22Z zombor $
10 * @copyright (c) 2007-2008 Kohana Team
11 * @license http://kohanaphp.com/license.html
13 class ORM_Tree
extends ORM
{
18 // Parent keyword name
19 protected $parent_key = 'parent_id';
22 * Overload ORM::__get to support "parent" and "children" properties.
24 * @param string column name
27 public function __get($column)
29 if ($column === 'parent')
31 if (empty($this->related
[$column]))
34 $model = ORM
::factory(inflector
::singular($this->children
));
36 if (array_key_exists($this->parent_key
, $this->object))
38 // Find children of this parent
39 $model->where($model->primary_key
, $this->object[$this->parent_key
])->find();
42 $this->related
[$column] = $model;
45 return $this->related
[$column];
47 elseif ($column === 'children')
49 if (empty($this->related
[$column]))
51 $model = ORM
::factory(inflector
::singular($this->children
));
53 if ($this->children
=== $this->table_name
)
55 // Load children within this table
56 $this->related
[$column] = $model
57 ->where($this->parent_key
, $this->object[$this->primary_key
])
62 // Find first selection of children
63 $this->related
[$column] = $model
64 ->where($this->foreign_key(), $this->object[$this->primary_key
])
65 ->where($this->parent_key
, NULL)
70 return $this->related
[$column];
73 return parent
::__get($column);