Initial Kohana install
[speedfreak] / Server / system / libraries / ORM_Tree.php
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3  * Object Relational Mapping (ORM) "tree" extension. Allows ORM objects to act
4  * as trees, with parents and children.
5  *
6  * $Id: ORM_Tree.php 3923 2009-01-22 15:37:04Z samsoir $
7  *
8  * @package    ORM
9  * @author     Kohana Team
10  * @copyright  (c) 2007-2008 Kohana Team
11  * @license    http://kohanaphp.com/license.html
12  */
13 class ORM_Tree_Core extends ORM {
14
15         // Name of the child
16         protected $ORM_Tree_children;
17
18         // Parent keyword name
19         protected $ORM_Tree_parent_key = 'parent_id';
20
21         /**
22          * Overload ORM::__get to support "parent" and "children" properties.
23          *
24          * @param   string  column name
25          * @return  mixed
26          */
27         public function __get($column)
28         {
29                 if ($column === 'parent')
30                 {
31                         if (empty($this->related[$column]))
32                         {
33                                 // Load child model
34                                 $model = ORM::factory(inflector::singular($this->ORM_Tree_children));
35
36                                 if (array_key_exists($this->ORM_Tree_parent_key, $this->object))
37                                 {
38                                         // Find children of this parent
39                                         $model->where($model->primary_key, $this->object[$this->ORM_Tree_parent_key])->find();
40                                 }
41
42                                 $this->related[$column] = $model;
43                         }
44
45                         return $this->related[$column];
46                 }
47                 elseif ($column === 'children')
48                 {
49                         if (empty($this->related[$column]))
50                         {
51                                 $model = ORM::factory(inflector::singular($this->ORM_Tree_children));
52
53                                 if ($this->ORM_Tree_children === $this->table_name)
54                                 {
55                                         // Load children within this table
56                                         $this->related[$column] = $model
57                                                 ->where($this->ORM_Tree_parent_key, $this->object[$this->primary_key])
58                                                 ->find_all();
59                                 }
60                                 else
61                                 {
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)
66                                                 ->find_all();
67                                 }
68                         }
69
70                         return $this->related[$column];
71                 }
72
73                 return parent::__get($column);
74         }
75
76 } // End ORM Tree