1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Database\Selector;
13:
14: use Nette;
15:
16:
17:
18: 19: 20: 21: 22: 23:
24: class TableRow extends Nette\Object implements \IteratorAggregate, \ArrayAccess
25: {
26:
27: protected $table;
28:
29:
30: protected $data;
31:
32:
33: private $modified = array();
34:
35:
36:
37: public function __construct(array $data, TableSelection $table)
38: {
39: $this->data = $data;
40: $this->table = $table;
41: }
42:
43:
44:
45: 46: 47: 48:
49: public function __toString()
50: {
51: return (string) $this[$this->table->primary]; 52: }
53:
54:
55:
56: 57: 58:
59: public function toArray()
60: {
61: $this->access(NULL);
62: return $this->data;
63: }
64:
65:
66:
67: 68: 69: 70: 71:
72: public function ref($name)
73: {
74: $referenced = $this->table->getReferencedTable($name, $column);
75: if (isset($referenced[$this[$column]])) { 76: $res = $referenced[$this[$column]];
77: return $res;
78: }
79: }
80:
81:
82:
83: 84: 85: 86: 87:
88: public function related($table)
89: {
90: $referencing = $this->table->getReferencingTable($table);
91: $referencing->active = $this[$this->table->primary];
92: return $referencing;
93: }
94:
95:
96:
97: 98: 99: 100: 101:
102: public function update($data = NULL)
103: {
104: if ($data === NULL) {
105: $data = $this->modified;
106: }
107: return $this->table->connection->table($this->table->name)
108: ->where($this->table->primary, $this[$this->table->primary])
109: ->update($data);
110: }
111:
112:
113:
114: 115: 116: 117:
118: public function delete()
119: {
120: return $this->table->connection->table($this->table->name)
121: ->where($this->table->primary, $this[$this->table->primary])
122: ->delete();
123: }
124:
125:
126:
127:
128:
129:
130:
131: public function getIterator()
132: {
133: $this->access(NULL);
134: return new \ArrayIterator($this->data);
135: }
136:
137:
138:
139:
140:
141:
142:
143: 144: 145: 146: 147:
148: public function offsetSet($key, $value)
149: {
150: $this->__set($key, $value);
151: }
152:
153:
154:
155: 156: 157: 158: 159:
160: public function offsetGet($key)
161: {
162: return $this->__get($key);
163: }
164:
165:
166:
167: 168: 169: 170: 171:
172: public function offsetExists($key)
173: {
174: return $this->__isset($key);
175: }
176:
177:
178:
179: 180: 181: 182: 183:
184: public function offsetUnset($key)
185: {
186: $this->__unset($key);
187: }
188:
189:
190:
191: public function __set($key, $value)
192: {
193: $this->data[$key] = $value;
194: $this->modified[$key] = $value;
195: }
196:
197:
198:
199: public function &__get($key)
200: {
201: if (array_key_exists($key, $this->data)) {
202: $this->access($key);
203: return $this->data[$key];
204: }
205:
206: $column = $this->table->connection->databaseReflection->getReferencedColumn($key, $this->table->name);
207: if (array_key_exists($column, $this->data)) {
208: $value = $this->data[$column];
209: $referenced = $this->table->getReferencedTable($key);
210: $ret = isset($referenced[$value]) ? $referenced[$value] : NULL; 211: return $ret;
212: }
213:
214: $this->access($key);
215: if (array_key_exists($key, $this->data)) {
216: return $this->data[$key];
217:
218: } else {
219: $this->access($key, TRUE);
220:
221: $this->access($column);
222: if (array_key_exists($column, $this->data)) {
223: $value = $this->data[$column];
224: $referenced = $this->table->getReferencedTable($key);
225: $ret = isset($referenced[$value]) ? $referenced[$value] : NULL; 226:
227: } else {
228: $this->access($column, TRUE);
229: trigger_error("Unknown column $key", E_USER_WARNING);
230: $ret = NULL;
231: }
232: return $ret;
233: }
234: }
235:
236:
237:
238: public function __isset($key)
239: {
240: $this->access($key);
241: $return = array_key_exists($key, $this->data);
242: if (!$return) {
243: $this->access($key, TRUE);
244: }
245: return $return;
246: }
247:
248:
249:
250: public function __unset($key)
251: {
252: unset($this->data[$key]);
253: unset($this->modified[$key]);
254: }
255:
256:
257:
258: public function access($key, $delete = FALSE)
259: {
260: if ($this->table->connection->cache && $this->table->access($key, $delete)) {
261: $this->data = $this->table[$this->data[$this->table->primary]]->data;
262: }
263: }
264:
265: }
266: