| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | class ORM_Iterator_Core implements Iterator, ArrayAccess, Countable { |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | protected $class_name; |
|---|
| 16 | protected $primary_key; |
|---|
| 17 | protected $primary_val; |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | protected $result; |
|---|
| 21 | |
|---|
| 22 | public function __construct(ORM $model, Database_Result $result) |
|---|
| 23 | { |
|---|
| 24 | |
|---|
| 25 | $this->class_name = get_class($model); |
|---|
| 26 | $this->primary_key = $model->primary_key; |
|---|
| 27 | $this->primary_val = $model->primary_val; |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | $this->result = $result->result(TRUE); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | public function as_array() |
|---|
| 39 | { |
|---|
| 40 | $array = array(); |
|---|
| 41 | |
|---|
| 42 | if ($results = $this->result->result_array()) |
|---|
| 43 | { |
|---|
| 44 | |
|---|
| 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 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 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 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | public function select_list($key = NULL, $val = NULL) |
|---|
| 79 | { |
|---|
| 80 | if ($key === NULL) |
|---|
| 81 | { |
|---|
| 82 | |
|---|
| 83 | $key = $this->primary_key; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | if ($val === NULL) |
|---|
| 87 | { |
|---|
| 88 | |
|---|
| 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 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | public function range($start, $end) |
|---|
| 108 | { |
|---|
| 109 | |
|---|
| 110 | $array = array(); |
|---|
| 111 | |
|---|
| 112 | if ($this->result->offsetExists($start)) |
|---|
| 113 | { |
|---|
| 114 | |
|---|
| 115 | $class = $this->class_name; |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | $end = $this->result->offsetExists($end) ? $end : $this->count(); |
|---|
| 119 | |
|---|
| 120 | for ($i = $start; $i < $end; $i++) |
|---|
| 121 | { |
|---|
| 122 | |
|---|
| 123 | $array[] = new $class($this->result->offsetGet($i)); |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | return $array; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | public function count() |
|---|
| 134 | { |
|---|
| 135 | return $this->result->count(); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | public function current() |
|---|
| 142 | { |
|---|
| 143 | if ($row = $this->result->current()) |
|---|
| 144 | { |
|---|
| 145 | |
|---|
| 146 | $class = $this->class_name; |
|---|
| 147 | |
|---|
| 148 | $row = new $class($row); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | return $row; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | public function key() |
|---|
| 158 | { |
|---|
| 159 | return $this->result->key(); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | public function next() |
|---|
| 166 | { |
|---|
| 167 | return $this->result->next(); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | public function rewind() |
|---|
| 174 | { |
|---|
| 175 | $this->result->rewind(); |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | public function valid() |
|---|
| 182 | { |
|---|
| 183 | return $this->result->valid(); |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | public function offsetExists($offset) |
|---|
| 190 | { |
|---|
| 191 | return $this->result->offsetExists($offset); |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | public function offsetGet($offset) |
|---|
| 198 | { |
|---|
| 199 | if ($this->result->offsetExists($offset)) |
|---|
| 200 | { |
|---|
| 201 | |
|---|
| 202 | $class = $this->class_name; |
|---|
| 203 | |
|---|
| 204 | return new $class($this->result->offsetGet($offset)); |
|---|
| 205 | } |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | public function offsetSet($offset, $value) |
|---|
| 214 | { |
|---|
| 215 | throw new Kohana_Database_Exception('database.result_read_only'); |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | public function offsetUnset($offset) |
|---|
| 224 | { |
|---|
| 225 | throw new Kohana_Database_Exception('database.result_read_only'); |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | } |
|---|