1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20: 21:
22: class TableRow extends Object implements IteratorAggregate, ArrayAccess
23: {
24:
25: private $table;
26:
27:
28: private $data;
29:
30:
31: private $modified = array();
32:
33:
34:
35: public function __construct(array $data, TableSelection $table)
36: {
37: $this->data = $data;
38: $this->table = $table;
39: }
40:
41:
42:
43: 44: 45: 46:
47: public function setTable(TableSelection $table)
48: {
49: $this->table = $table;
50: }
51:
52:
53:
54: 55: 56: 57:
58: public function getTable()
59: {
60: return $this->table;
61: }
62:
63:
64:
65: public function __toString()
66: {
67: try {
68: return (string) $this->getPrimary();
69: } catch (Exception $e) {
70: Debugger::toStringException($e);
71: }
72: }
73:
74:
75:
76: 77: 78:
79: public function toArray()
80: {
81: $this->access(NULL);
82: return $this->data;
83: }
84:
85:
86:
87: 88: 89: 90:
91: public function getPrimary()
92: {
93: if (!isset($this->data[$this->table->getPrimary()])) {
94: throw new NotSupportedException("Table {$this->table->getName()} does not have any primary key.");
95: }
96: return $this[$this->table->getPrimary()];
97: }
98:
99:
100:
101: 102: 103: 104: 105: 106:
107: public function ref($key, $throughColumn = NULL)
108: {
109: if (!$throughColumn) {
110: list($key, $throughColumn) = $this->table->getConnection()->getDatabaseReflection()->getBelongsToReference($this->table->getName(), $key);
111: }
112:
113: return $this->getReference($key, $throughColumn);
114: }
115:
116:
117:
118: 119: 120: 121: 122: 123:
124: public function related($key, $throughColumn = NULL)
125: {
126: if (strpos($key, '.') !== FALSE) {
127: list($key, $throughColumn) = explode('.', $key);
128: } elseif (!$throughColumn) {
129: list($key, $throughColumn) = $this->table->getConnection()->getDatabaseReflection()->getHasManyReference($this->table->getName(), $key);
130: }
131:
132: return $this->table->getReferencingTable($key, $throughColumn, $this[$this->table->getPrimary()]);
133: }
134:
135:
136:
137: 138: 139: 140: 141:
142: public function update($data = NULL)
143: {
144: if ($data === NULL) {
145: $data = $this->modified;
146: }
147: return $this->table->getConnection()->table($this->table->getName())
148: ->where($this->table->getPrimary(), $this[$this->table->getPrimary()])
149: ->update($data);
150: }
151:
152:
153:
154: 155: 156: 157:
158: public function delete()
159: {
160: return $this->table->getConnection()->table($this->table->getName())
161: ->where($this->table->getPrimary(), $this[$this->table->getPrimary()])
162: ->delete();
163: }
164:
165:
166:
167:
168:
169:
170:
171: public function getIterator()
172: {
173: $this->access(NULL);
174: return new ArrayIterator($this->data);
175: }
176:
177:
178:
179:
180:
181:
182:
183: 184: 185: 186: 187: 188:
189: public function offsetSet($key, $value)
190: {
191: $this->__set($key, $value);
192: }
193:
194:
195:
196: 197: 198: 199: 200:
201: public function offsetGet($key)
202: {
203: return $this->__get($key);
204: }
205:
206:
207:
208: 209: 210: 211: 212:
213: public function offsetExists($key)
214: {
215: return $this->__isset($key);
216: }
217:
218:
219:
220: 221: 222: 223: 224:
225: public function offsetUnset($key)
226: {
227: $this->__unset($key);
228: }
229:
230:
231:
232: public function __set($key, $value)
233: {
234: $this->data[$key] = $value;
235: $this->modified[$key] = $value;
236: }
237:
238:
239:
240: public function &__get($key)
241: {
242: $this->access($key);
243: if (array_key_exists($key, $this->data)) {
244: return $this->data[$key];
245: }
246:
247: list($table, $column) = $this->table->getConnection()->getDatabaseReflection()->getBelongsToReference($this->table->getName(), $key);
248: $referenced = $this->getReference($table, $column);
249: if ($referenced !== FALSE) {
250: $this->access($key, FALSE);
251: return $referenced;
252: }
253:
254: $this->access($key, NULL);
255: throw new MemberAccessException("Cannot read an undeclared column \"$key\".");
256: }
257:
258:
259:
260: public function __isset($key)
261: {
262: $this->access($key);
263: if (array_key_exists($key, $this->data)) {
264: return isset($this->data[$key]);
265: }
266: $this->access($key, NULL);
267: return FALSE;
268: }
269:
270:
271:
272: public function __unset($key)
273: {
274: unset($this->data[$key]);
275: unset($this->modified[$key]);
276: }
277:
278:
279:
280: 281: 282:
283: public function access($key, $cache = TRUE)
284: {
285: if ($this->table->getConnection()->getCache() && !isset($this->modified[$key]) && $this->table->access($key, $cache)) {
286: $id = (isset($this->data[$this->table->getPrimary()]) ? $this->data[$this->table->getPrimary()] : $this->data);
287: $this->data = $this->table[$id]->data;
288: }
289: }
290:
291:
292:
293: protected function getReference($table, $column)
294: {
295: if (array_key_exists($column, $this->data)) {
296: $this->access($column);
297:
298: $value = $this->data[$column];
299: $value = $value instanceof TableRow ? $value->getPrimary() : $value;
300:
301: $referenced = $this->table->getReferencedTable($table, $column, !empty($this->modified[$column]));
302: $referenced = isset($referenced[$value]) ? $referenced[$value] : NULL;
303:
304: if (!empty($this->modified[$column])) {
305: $this->modified[$column] = 0;
306: }
307:
308: return $referenced;
309: }
310:
311: return FALSE;
312: }
313:
314: }
315: