Merge branch 'maint/7.0'
[ninja.git] / system / libraries / ORM_Iterator.php
blob7a2c9eedb4e1f4ea44e82ba58c89ce046c43c356
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3 * Object Relational Mapping (ORM) result iterator.
5 * $Id: ORM_Iterator.php 3917 2009-01-21 03:06:22Z zombor $
7 * @package ORM
8 * @author Kohana Team
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
12 class ORM_Iterator implements Iterator, ArrayAccess, Countable {
14 // Class attributes
15 protected $class_name;
16 protected $primary_key;
17 protected $primary_val;
19 // Database result object
20 protected $result;
22 public function __construct(ORM $model, Database_Result $result)
24 // Class attributes
25 $this->class_name = get_class($model);
26 $this->primary_key = $model->primary_key;
27 $this->primary_val = $model->primary_val;
29 // Database result
30 $this->result = $result->result(TRUE);
33 /**
34 * Returns an array of the results as ORM objects.
36 * @return array
38 public function as_array()
40 $array = array();
42 if ($results = $this->result->result_array())
44 // Import class name
45 $class = $this->class_name;
47 foreach ($results as $obj)
49 $array[] = new $class($obj);
53 return $array;
56 /**
57 * Return an array of all of the primary keys for this object.
59 * @return array
61 public function primary_key_array()
63 $ids = array();
64 foreach ($this->result as $row)
66 $ids[] = $row->{$this->primary_key};
68 return $ids;
71 /**
72 * Create a key/value array from the results.
74 * @param string key column
75 * @param string value column
76 * @return array
78 public function select_list($key = NULL, $val = NULL)
80 if ($key === NULL)
82 // Use the default key
83 $key = $this->primary_key;
86 if ($val === NULL)
88 // Use the default value
89 $val = $this->primary_val;
92 $array = array();
93 foreach ($this->result->result_array() as $row)
95 $array[$row->$key] = $row->$val;
97 return $array;
101 * Return a range of offsets.
103 * @param integer start
104 * @param integer end
105 * @return array
107 public function range($start, $end)
109 // Array of objects
110 $array = array();
112 if ($this->result->offsetExists($start))
114 // Import the class name
115 $class = $this->class_name;
117 // Set the end offset
118 $end = $this->result->offsetExists($end) ? $end : $this->count();
120 for ($i = $start; $i < $end; $i++)
122 // Insert each object in the range
123 $array[] = new $class($this->result->offsetGet($i));
127 return $array;
131 * Countable: count
133 public function count()
135 return $this->result->count();
139 * Iterator: current
141 public function current()
143 if ($row = $this->result->current())
145 // Import class name
146 $class = $this->class_name;
148 $row = new $class($row);
151 return $row;
155 * Iterator: key
157 public function key()
159 return $this->result->key();
163 * Iterator: next
165 public function next()
167 return $this->result->next();
171 * Iterator: rewind
173 public function rewind()
175 $this->result->rewind();
179 * Iterator: valid
181 public function valid()
183 return $this->result->valid();
187 * ArrayAccess: offsetExists
189 public function offsetExists($offset)
191 return $this->result->offsetExists($offset);
195 * ArrayAccess: offsetGet
197 public function offsetGet($offset)
199 if ($this->result->offsetExists($offset))
201 // Import class name
202 $class = $this->class_name;
204 return new $class($this->result->offsetGet($offset));
209 * ArrayAccess: offsetSet
211 * @throws Kohana_Database_Exception
213 public function offsetSet($offset, $value)
215 throw new Kohana_Database_Exception('database.result_read_only');
219 * ArrayAccess: offsetUnset
221 * @throws Kohana_Database_Exception
223 public function offsetUnset($offset)
225 throw new Kohana_Database_Exception('database.result_read_only');
228 } // End ORM Iterator