Packages

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

  • NGroupedTableSelection
  • NTableRow
  • NTableSelection
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  * @package Nette\Database\Table
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * Single row representation.
 17:  * ActiveRow is based on the great library NotORM http://www.notorm.com written by Jakub Vrana.
 18:  *
 19:  * @author     Jakub Vrana
 20:  * @package Nette\Database\Table
 21:  */
 22: class NTableRow extends NObject implements IteratorAggregate, ArrayAccess
 23: {
 24:     /** @var NTableSelection */
 25:     private $table;
 26: 
 27:     /** @var array of row data */
 28:     private $data;
 29: 
 30:     /** @var array of new values {@see NTableRow::update()} */
 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:      * @internal
 45:      * @ignore
 46:      */
 47:     public function getTable()
 48:     {
 49:         return $this->table;
 50:     }
 51: 
 52: 
 53: 
 54:     public function __toString()
 55:     {
 56:         try {
 57:             return (string) $this->getPrimary();
 58:         } catch (Exception $e) {
 59:             NDebugger::toStringException($e);
 60:         }
 61:     }
 62: 
 63: 
 64: 
 65:     /**
 66:      * @return array
 67:      */
 68:     public function toArray()
 69:     {
 70:         $this->access(NULL);
 71:         return $this->data;
 72:     }
 73: 
 74: 
 75: 
 76:     /**
 77:      * Returns primary key value.
 78:      * @return mixed
 79:      */
 80:     public function getPrimary()
 81:     {
 82:         if (!isset($this->data[$this->table->getPrimary()])) {
 83:             throw new NotSupportedException("Table {$this->table->getName()} does not have any primary key.");
 84:         }
 85:         return $this[$this->table->getPrimary()];
 86:     }
 87: 
 88: 
 89: 
 90:     /**
 91:      * Returns referenced row.
 92:      * @param  string
 93:      * @param  string
 94:      * @return NTableRow or NULL if the row does not exist
 95:      */
 96:     public function ref($key, $throughColumn = NULL)
 97:     {
 98:         if (!$throughColumn) {
 99:             list($key, $throughColumn) = $this->table->getConnection()->getDatabaseReflection()->getBelongsToReference($this->table->getName(), $key);
100:         }
101: 
102:         return $this->getReference($key, $throughColumn);
103:     }
104: 
105: 
106: 
107:     /**
108:      * Returns referencing rows.
109:      * @param  string
110:      * @param  string
111:      * @param  bool
112:      * @return NGroupedTableSelection
113:      */
114:     public function related($key, $throughColumn = NULL, $forceNewInstance = FALSE)
115:     {
116:         if (strpos($key, '.') !== FALSE) {
117:             list($key, $throughColumn) = explode('.', $key);
118:         } elseif (!is_string($throughColumn)) {
119:             list($key, $throughColumn) = $this->table->getConnection()->getDatabaseReflection()->getHasManyReference($this->table->getName(), $key);
120:         }
121: 
122:         return $this->table->getReferencingTable($key, $throughColumn, $this[$this->table->getPrimary()], $forceNewInstance);
123:     }
124: 
125: 
126: 
127:     /**
128:      * Updates row.
129:      * @param  array or NULL for all modified values
130:      * @return int number of affected rows or FALSE in case of an error
131:      */
132:     public function update($data = NULL)
133:     {
134:         if ($data === NULL) {
135:             $data = $this->modified;
136:         }
137:         return $this->table->getConnection()->table($this->table->getName())
138:             ->where($this->table->getPrimary(), $this[$this->table->getPrimary()])
139:             ->update($data);
140:     }
141: 
142: 
143: 
144:     /**
145:      * Deletes row.
146:      * @return int number of affected rows or FALSE in case of an error
147:      */
148:     public function delete()
149:     {
150:         return $this->table->getConnection()->table($this->table->getName())
151:             ->where($this->table->getPrimary(), $this[$this->table->getPrimary()])
152:             ->delete();
153:     }
154: 
155: 
156: 
157:     /********************* interface IteratorAggregate ****************d*g**/
158: 
159: 
160: 
161:     public function getIterator()
162:     {
163:         $this->access(NULL);
164:         return new ArrayIterator($this->data);
165:     }
166: 
167: 
168: 
169:     /********************* interface ArrayAccess & magic accessors ****************d*g**/
170: 
171: 
172: 
173:     /**
174:      * Stores value in column.
175:      * @param  string column name
176:      * @param  string value
177:      * @return void
178:      */
179:     public function offsetSet($key, $value)
180:     {
181:         $this->__set($key, $value);
182:     }
183: 
184: 
185: 
186:     /**
187:      * Returns value of column.
188:      * @param  string column name
189:      * @return string
190:      */
191:     public function offsetGet($key)
192:     {
193:         return $this->__get($key);
194:     }
195: 
196: 
197: 
198:     /**
199:      * Tests if column exists.
200:      * @param  string column name
201:      * @return bool
202:      */
203:     public function offsetExists($key)
204:     {
205:         return $this->__isset($key);
206:     }
207: 
208: 
209: 
210:     /**
211:      * Removes column from data.
212:      * @param  string column name
213:      * @return void
214:      */
215:     public function offsetUnset($key)
216:     {
217:         $this->__unset($key);
218:     }
219: 
220: 
221: 
222:     public function __set($key, $value)
223:     {
224:         $this->data[$key] = $value;
225:         $this->modified[$key] = $value;
226:     }
227: 
228: 
229: 
230:     public function &__get($key)
231:     {
232:         $this->access($key);
233:         if (array_key_exists($key, $this->data)) {
234:             return $this->data[$key];
235:         }
236: 
237:         list($table, $column) = $this->table->getConnection()->getDatabaseReflection()->getBelongsToReference($this->table->getName(), $key);
238:         $referenced = $this->getReference($table, $column);
239:         if ($referenced !== FALSE) {
240:             $this->access($key, FALSE);
241:             return $referenced;
242:         }
243: 
244:         $this->access($key, NULL);
245:         throw new MemberAccessException("Cannot read an undeclared column \"$key\".");
246:     }
247: 
248: 
249: 
250:     public function __isset($key)
251:     {
252:         $this->access($key);
253:         if (array_key_exists($key, $this->data)) {
254:             return isset($this->data[$key]);
255:         }
256:         $this->access($key, NULL);
257:         return FALSE;
258:     }
259: 
260: 
261: 
262:     public function __unset($key)
263:     {
264:         unset($this->data[$key]);
265:         unset($this->modified[$key]);
266:     }
267: 
268: 
269: 
270:     /**
271:      * @internal
272:      */
273:     public function access($key, $cache = TRUE)
274:     {
275:         if ($this->table->getConnection()->getCache() && !isset($this->modified[$key]) && $this->table->access($key, $cache)) {
276:             $id = (isset($this->data[$this->table->getPrimary()]) ? $this->data[$this->table->getPrimary()] : $this->data);
277:             $this->data = $this->table[$id]->data;
278:         }
279:     }
280: 
281: 
282: 
283:     protected function getReference($table, $column)
284:     {
285:         if (array_key_exists($column, $this->data)) {
286:             $this->access($column);
287: 
288:             $value = $this->data[$column];
289:             $value = $value instanceof NTableRow ? $value->getPrimary() : $value;
290: 
291:             $referenced = $this->table->getReferencedTable($table, $column, !empty($this->modified[$column]));
292:             $referenced = isset($referenced[$value]) ? $referenced[$value] : NULL; // referenced row may not exist
293: 
294:             if (!empty($this->modified[$column])) { // cause saving changed column and prevent regenerating referenced table for $column
295:                 $this->modified[$column] = 0; // 0 fails on empty, pass on isset
296:             }
297: 
298:             return $referenced;
299:         }
300: 
301:         return FALSE;
302:     }
303: 
304: }
305: 
Nette Framework 2.0.3 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.7.0