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