1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
3 * Object Relational Mapping (ORM) result iterator.
5 * $Id: ORM_Iterator.php 3917 2009-01-21 03:06:22Z zombor $
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
12 class ORM_Iterator
implements Iterator
, ArrayAccess
, Countable
{
15 protected $class_name;
16 protected $primary_key;
17 protected $primary_val;
19 // Database result object
22 public function __construct(ORM
$model, Database_Result
$result)
25 $this->class_name
= get_class($model);
26 $this->primary_key
= $model->primary_key
;
27 $this->primary_val
= $model->primary_val
;
30 $this->result
= $result->result(TRUE);
34 * Returns an array of the results as ORM objects.
38 public function as_array()
42 if ($results = $this->result
->result_array())
45 $class = $this->class_name
;
47 foreach ($results as $obj)
49 $array[] = new $class($obj);
57 * Return an array of all of the primary keys for this object.
61 public function primary_key_array()
64 foreach ($this->result
as $row)
66 $ids[] = $row->{$this->primary_key
};
72 * Create a key/value array from the results.
74 * @param string key column
75 * @param string value column
78 public function select_list($key = NULL, $val = NULL)
82 // Use the default key
83 $key = $this->primary_key
;
88 // Use the default value
89 $val = $this->primary_val
;
93 foreach ($this->result
->result_array() as $row)
95 $array[$row->$key] = $row->$val;
101 * Return a range of offsets.
103 * @param integer start
107 public function range($start, $end)
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));
133 public function count()
135 return $this->result
->count();
141 public function current()
143 if ($row = $this->result
->current())
146 $class = $this->class_name
;
148 $row = new $class($row);
157 public function key()
159 return $this->result
->key();
165 public function next()
167 return $this->result
->next();
173 public function rewind()
175 $this->result
->rewind();
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))
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