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)->where($this->table->primary, $this[$this->table->primary])->update($data);
108: }
109:
110:
111:
112: 113: 114: 115:
116: public function delete()
117: {
118: return $this->table->connection->table($this->table->name)->where($this->table->primary, $this[$this->table->primary])->delete();
119: }
120:
121:
122:
123:
124:
125:
126:
127: public function getIterator()
128: {
129: $this->access(NULL);
130: return new \ArrayIterator($this->data);
131: }
132:
133:
134:
135:
136:
137:
138:
139: 140: 141: 142: 143:
144: public function offsetSet($key, $value)
145: {
146: $this->__set($key, $value);
147: }
148:
149:
150:
151: 152: 153: 154: 155:
156: public function offsetGet($key)
157: {
158: return $this->__get($key);
159: }
160:
161:
162:
163: 164: 165: 166: 167:
168: public function offsetExists($key)
169: {
170: return $this->__isset($key);
171: }
172:
173:
174:
175: 176: 177: 178: 179:
180: public function offsetUnset($key)
181: {
182: $this->__unset($key);
183: }
184:
185:
186:
187: public function __set($key, $value)
188: {
189: $this->data[$key] = $value;
190: $this->modified[$key] = $value;
191: }
192:
193:
194:
195: public function &__get($key)
196: {
197: if (array_key_exists($key, $this->data)) {
198: $this->access($key);
199: return $this->data[$key];
200: }
201:
202: $column = $this->table->connection->databaseReflection->getReferencedColumn($key, $this->table->name);
203: if (array_key_exists($column, $this->data)) {
204: $value = $this->data[$column];
205: $referenced = $this->table->getReferencedTable($key);
206: $ret = isset($referenced[$value]) ? $referenced[$value] : NULL; 207: return $ret;
208: }
209:
210: $this->access($key);
211: if (array_key_exists($key, $this->data)) {
212: return $this->data[$key];
213:
214: } else {
215: $this->access($key, TRUE);
216:
217: $this->access($column);
218: if (array_key_exists($column, $this->data)) {
219: $value = $this->data[$column];
220: $referenced = $this->table->getReferencedTable($key);
221: $ret = isset($referenced[$value]) ? $referenced[$value] : NULL; 222:
223: } else {
224: $this->access($column, TRUE);
225: trigger_error("Unknown column $key", E_USER_WARNING);
226: $ret = NULL;
227: }
228: return $ret;
229: }
230: }
231:
232:
233:
234: public function __isset($key)
235: {
236: $this->access($key);
237: $return = array_key_exists($key, $this->data);
238: if (!$return) {
239: $this->access($key, TRUE);
240: }
241: return $return;
242: }
243:
244:
245:
246: public function __unset($key)
247: {
248: unset($this->data[$key]);
249: unset($this->modified[$key]);
250: }
251:
252:
253:
254: public function access($key, $delete = FALSE)
255: {
256: if ($this->table->connection->cache && $this->table->access($key, $delete)) {
257: $this->data = $this->table[$this->data[$this->table->primary]]->data;
258: }
259: }
260:
261: }
262: