blob: 0bf2b47785f992c8d9dcf162fd5094aa6e928762 (
plain)
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* Object Relational Mapping (ORM) result iterator.
*
* @package Kohana
* @author Kohana Team
* @copyright (c) 2007-2009 Kohana Team
* @license http://kohanaphp.com/license
*/
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 (make sure rows are returned as arrays)
$this->result = $result;
}
/**
* Returns an array of the results as ORM objects or a nested array
*
* @param bool TRUE to return an array of ORM objects, FALSE for an array of arrays
* @param string key column to index on, NULL to ignore
* @return array
*/
public function as_array($objects = TRUE, $key = NULL)
{
$array = array();
// Import class name
$class = $this->class_name;
if ($objects)
{
// Generate an array of objects
foreach ($this->result as $data)
{
if ($key === NULL)
{
// No indexing
$array[] = new $class($data);
}
else
{
// Index on the given key
$array[$data->$key] = new $class($data);
}
}
}
else
{
// Generate an array of arrays (and the subarrays may be nested in the case of relationships)
// This could be done by creating a new ORM object and calling as_array on it, but this is much faster
foreach ($this->result as $data)
{
// Have to do a bit of magic here to handle any relationships and generate a nested array for them
$temp = array();
foreach ($data as $key => $val)
{
$ptr = & $temp;
foreach (explode(':', $key) as $subkey)
{
// Walk thru the relationships (separated in the key name by a ':')
// 'user:email:address' will be array['user']['email']['address']
$ptr = & $ptr[$subkey];
}
// Set the value
$ptr = $val;
}
// Append the result
$array[] = $temp;
}
}
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 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
|