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