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