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