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