root/trunk/system/libraries/ORM_Iterator.php

Revision 3700, 3.8 kB (checked in by Shadowhand, 12 days ago)

Updates to trunk:

  • Removed all SYSPATH file checks
  • Deleted some modules: code_coverage (3.0), shoutbox (defunct), user_guide (defunct), kobot (3.0), object_db (3.0)
  • Updated ORM and ORM_Iterator to match 3.0, enabling the new HABTM stuff, see r3636 and r3640
  • Property svn:eol-style set to LF
  • Property copyright set to Copyright (c) 2007-2008 Kohana Team
  • Property svn:keywords set to Id
Line 
1<?php
2/**
3* Object Relational Mapping (ORM) result iterator.
4*
5* $Id$
6*
7* @package    ORM
8* @author     Kohana Team
9* @copyright  (c) 2007-2008 Kohana Team
10* @license    http://kohanaphp.com/license.html
11*/
12class ORM_Iterator_Core implements Iterator, ArrayAccess, Countable {
13
14    // Class attributes
15    protected $class_name;
16    protected $primary_key;
17    protected $primary_val;
18
19    // Database result object
20    protected $result;
21
22    public function __construct(ORM $model, Database_Result $result)
23    {
24        // Class attributes
25        $this->class_name  = get_class($model);
26        $this->primary_key = $model->primary_key;
27        $this->primary_val = $model->primary_val;
28
29        // Database result
30        $this->result = $result->result(TRUE);
31    }
32
33    /**
34     * Returns an array of the results as ORM objects.
35     *
36     * @return  array
37     */
38    public function as_array()
39    {
40        $array = array();
41
42        if ($results = $this->result->result_array())
43        {
44            // Import class name
45            $class = $this->class_name;
46
47            foreach ($results as $obj)
48            {
49                $array[] = new $class($obj);
50            }
51        }
52
53        return $array;
54    }
55
56    /**
57     * Return an array of all of the primary keys for this object.
58     *
59     * @return  array
60     */
61    public function primary_key_array()
62    {
63        $ids = array();
64        foreach ($this->result as $row)
65        {
66            $ids[] = $row->{$this->primary_key};
67        }
68        return $ids;
69    }
70
71    /**
72     * Create a key/value array from the results.
73     *
74     * @param   string  key column
75     * @param   string  value column
76     * @return  array
77     */
78    public function select_list($key = NULL, $val = NULL)
79    {
80        if ($key === NULL)
81        {
82            // Use the default key
83            $key = $this->primary_key;
84        }
85
86        if ($val === NULL)
87        {
88            // Use the default value
89            $val = $this->primary_val;
90        }
91
92        $array = array();
93        foreach ($this->result->result_array() as $row)
94        {
95            $array[$row->$key] = $row->$val;
96        }
97        return $array;
98    }
99
100    /**
101     * Return a range of offsets.
102     *
103     * @param   integer  start
104     * @param   integer  end
105     * @return  array
106     */
107    public function range($start, $end)
108    {
109        // Array of objects
110        $array = array();
111
112        if ($this->result->offsetExists($start))
113        {
114            // Import the class name
115            $class = $this->class_name;
116
117            // Set the end offset
118            $end = $this->result->offsetExists($end) ? $end : $this->count();
119
120            for ($i = $start; $i < $end; $i++)
121            {
122                // Insert each object in the range
123                $array[] = new $class($this->result->offsetGet($i));
124            }
125        }
126
127        return $array;
128    }
129
130    /**
131     * Countable: count
132     */
133    public function count()
134    {
135        return $this->result->count();
136    }
137
138    /**
139     * Iterator: current
140     */
141    public function current()
142    {
143        if ($row = $this->result->current())
144        {
145            // Import class name
146            $class = $this->class_name;
147
148            $row = new $class($row);
149        }
150
151        return $row;
152    }
153
154    /**
155     * Iterator: key
156     */
157    public function key()
158    {
159        return $this->result->key();
160    }
161
162    /**
163     * Iterator: next
164     */
165    public function next()
166    {
167        return $this->result->next();
168    }
169
170    /**
171     * Iterator: rewind
172     */
173    public function rewind()
174    {
175        $this->result->rewind();
176    }
177
178    /**
179     * Iterator: valid
180     */
181    public function valid()
182    {
183        return $this->result->valid();
184    }
185
186    /**
187     * ArrayAccess: offsetExists
188     */
189    public function offsetExists($offset)
190    {
191        return $this->result->offsetExists($offset);
192    }
193
194    /**
195     * ArrayAccess: offsetGet
196     */
197    public function offsetGet($offset)
198    {
199        if ($this->result->offsetExists($offset))
200        {
201            // Import class name
202            $class = $this->class_name;
203
204            return new $class($this->result->offsetGet($offset));
205        }
206    }
207
208    /**
209     * ArrayAccess: offsetSet
210     *
211     * @throws  Kohana_Database_Exception
212     */
213    public function offsetSet($offset, $value)
214    {
215        throw new Kohana_Database_Exception('database.result_read_only');
216    }
217
218    /**
219     * ArrayAccess: offsetUnset
220     *
221     * @throws  Kohana_Database_Exception
222     */
223    public function offsetUnset($offset)
224    {
225        throw new Kohana_Database_Exception('database.result_read_only');
226    }
227
228} // End ORM Iterator
Note: See TracBrowser for help on using the browser.