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 3923 2009-01-22 15:37:04Z samsoir $
10 * @copyright (c) 2007-2008 Kohana Team
11 * @license http://kohanaphp.com/license.html
13 class ORM_Tree_Core extends ORM {
16 protected $ORM_Tree_children;
18 // Parent keyword name
19 protected $ORM_Tree_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->ORM_Tree_children));
36 if (array_key_exists($this->ORM_Tree_parent_key, $this->object))
38 // Find children of this parent
39 $model->where($model->primary_key, $this->object[$this->ORM_Tree_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->ORM_Tree_children));
53 if ($this->ORM_Tree_children === $this->table_name)
55 // Load children within this table
56 $this->related[$column] = $model
57 ->where($this->ORM_Tree_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->ORM_Tree_parent_key, NULL)
70 return $this->related[$column];
73 return parent::__get($column);