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

  • GroupedTableSelection
  • SqlBuilder
  • TableRow
  • TableSelection
  • 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 TableRow extends Object implements IteratorAggregate, ArrayAccess
 23: {
 24:     /** @var TableSelection */
 25:     private $table;
 26: 
 27:     /** @var array of row data */
 28:     private $data;
 29: 
 30:     /** @var array of new values {@see TableRow::update()} */
 31:     private $modified = array();
 32: 
 33: 
 34: 
 35:     public function __construct(array $data, TableSelection $table)
 36:     {
 37:         $this->data = $data;
 38:         $this->table = $table;
 39:     }
 40: 
 41: 
 42: 
 43:     /**
 44:      * @internal
 45:      * @ignore
 46:      */
 47:     public function setTable(TableSelection $table)
 48:     {
 49:         $this->table = $table;
 50:     }
 51: 
 52: 
 53: 
 54:     /**
 55:      * @internal
 56:      * @ignore
 57:      */
 58:     public function getTable()
 59:     {
 60:         return $this->table;
 61:     }
 62: 
 63: 
 64: 
 65:     public function __toString()
 66:     {
 67:         try {
 68:             return (string) $this->getPrimary();
 69:         } catch (Exception $e) {
 70:             Debugger::toStringException($e);
 71:         }
 72:     }
 73: 
 74: 
 75: 
 76:     /**
 77:      * @return array
 78:      */
 79:     public function toArray()
 80:     {
 81:         $this->access(NULL);
 82:         return $this->data;
 83:     }
 84: 
 85: 
 86: 
 87:     /**
 88:      * Returns primary key value.
 89:      * @return mixed
 90:      */
 91:     public function getPrimary()
 92:     {
 93:         if (!isset($this->data[$this->table->getPrimary()])) {
 94:             throw new NotSupportedException("Table {$this->table->getName()} does not have any primary key.");
 95:         }
 96:         return $this[$this->table->getPrimary()];
 97:     }
 98: 
 99: 
100: 
101:     /**
102:      * Returns referenced row.
103:      * @param  string
104:      * @param  string
105:      * @return TableRow or NULL if the row does not exist
106:      */
107:     public function ref($key, $throughColumn = NULL)
108:     {
109:         if (!$throughColumn) {
110:             list($key, $throughColumn) = $this->table->getConnection()->getDatabaseReflection()->getBelongsToReference($this->table->getName(), $key);
111:         }
112: 
113:         return $this->getReference($key, $throughColumn);
114:     }
115: 
116: 
117: 
118:     /**
119:      * Returns referencing rows.
120:      * @param  string
121:      * @param  string
122:      * @return GroupedTableSelection
123:      */
124:     public function related($key, $throughColumn = NULL)
125:     {
126:         if (strpos($key, '.') !== FALSE) {
127:             list($key, $throughColumn) = explode('.', $key);
128:         } elseif (!$throughColumn) {
129:             list($key, $throughColumn) = $this->table->getConnection()->getDatabaseReflection()->getHasManyReference($this->table->getName(), $key);
130:         }
131: 
132:         return $this->table->getReferencingTable($key, $throughColumn, $this[$this->table->getPrimary()]);
133:     }
134: 
135: 
136: 
137:     /**
138:      * Updates row.
139:      * @param  array or NULL for all modified values
140:      * @return int number of affected rows or FALSE in case of an error
141:      */
142:     public function update($data = NULL)
143:     {
144:         if ($data === NULL) {
145:             $data = $this->modified;
146:         }
147:         return $this->table->getConnection()->table($this->table->getName())
148:             ->where($this->table->getPrimary(), $this[$this->table->getPrimary()])
149:             ->update($data);
150:     }
151: 
152: 
153: 
154:     /**
155:      * Deletes row.
156:      * @return int number of affected rows or FALSE in case of an error
157:      */
158:     public function delete()
159:     {
160:         return $this->table->getConnection()->table($this->table->getName())
161:             ->where($this->table->getPrimary(), $this[$this->table->getPrimary()])
162:             ->delete();
163:     }
164: 
165: 
166: 
167:     /********************* interface IteratorAggregate ****************d*g**/
168: 
169: 
170: 
171:     public function getIterator()
172:     {
173:         $this->access(NULL);
174:         return new ArrayIterator($this->data);
175:     }
176: 
177: 
178: 
179:     /********************* interface ArrayAccess & magic accessors ****************d*g**/
180: 
181: 
182: 
183:     /**
184:      * Stores value in column.
185:      * @param  string column name
186:      * @param  string value
187:      * @return void
188:      */
189:     public function offsetSet($key, $value)
190:     {
191:         $this->__set($key, $value);
192:     }
193: 
194: 
195: 
196:     /**
197:      * Returns value of column.
198:      * @param  string column name
199:      * @return string
200:      */
201:     public function offsetGet($key)
202:     {
203:         return $this->__get($key);
204:     }
205: 
206: 
207: 
208:     /**
209:      * Tests if column exists.
210:      * @param  string column name
211:      * @return bool
212:      */
213:     public function offsetExists($key)
214:     {
215:         return $this->__isset($key);
216:     }
217: 
218: 
219: 
220:     /**
221:      * Removes column from data.
222:      * @param  string column name
223:      * @return void
224:      */
225:     public function offsetUnset($key)
226:     {
227:         $this->__unset($key);
228:     }
229: 
230: 
231: 
232:     public function __set($key, $value)
233:     {
234:         $this->data[$key] = $value;
235:         $this->modified[$key] = $value;
236:     }
237: 
238: 
239: 
240:     public function &__get($key)
241:     {
242:         $this->access($key);
243:         if (array_key_exists($key, $this->data)) {
244:             return $this->data[$key];
245:         }
246: 
247:         list($table, $column) = $this->table->getConnection()->getDatabaseReflection()->getBelongsToReference($this->table->getName(), $key);
248:         $referenced = $this->getReference($table, $column);
249:         if ($referenced !== FALSE) {
250:             $this->access($key, FALSE);
251:             return $referenced;
252:         }
253: 
254:         $this->access($key, NULL);
255:         throw new MemberAccessException("Cannot read an undeclared column \"$key\".");
256:     }
257: 
258: 
259: 
260:     public function __isset($key)
261:     {
262:         $this->access($key);
263:         if (array_key_exists($key, $this->data)) {
264:             return isset($this->data[$key]);
265:         }
266:         $this->access($key, NULL);
267:         return FALSE;
268:     }
269: 
270: 
271: 
272:     public function __unset($key)
273:     {
274:         unset($this->data[$key]);
275:         unset($this->modified[$key]);
276:     }
277: 
278: 
279: 
280:     /**
281:      * @internal
282:      */
283:     public function access($key, $cache = TRUE)
284:     {
285:         if ($this->table->getConnection()->getCache() && !isset($this->modified[$key]) && $this->table->access($key, $cache)) {
286:             $id = (isset($this->data[$this->table->getPrimary()]) ? $this->data[$this->table->getPrimary()] : $this->data);
287:             $this->data = $this->table[$id]->data;
288:         }
289:     }
290: 
291: 
292: 
293:     protected function getReference($table, $column)
294:     {
295:         if (array_key_exists($column, $this->data)) {
296:             $this->access($column);
297: 
298:             $value = $this->data[$column];
299:             $value = $value instanceof TableRow ? $value->getPrimary() : $value;
300: 
301:             $referenced = $this->table->getReferencedTable($table, $column, !empty($this->modified[$column]));
302:             $referenced = isset($referenced[$value]) ? $referenced[$value] : NULL; // referenced row may not exist
303: 
304:             if (!empty($this->modified[$column])) { // cause saving changed column and prevent regenerating referenced table for $column
305:                 $this->modified[$column] = 0; // 0 fails on empty, pass on isset
306:             }
307: 
308:             return $referenced;
309:         }
310: 
311:         return FALSE;
312:     }
313: 
314: }
315: 
Nette Framework 2.0.5 (for PHP 5.2, un-prefixed) API API documentation generated by ApiGen 2.7.0