Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy

Classes

  • ActiveRow
  • GroupedSelection
  • Selection
  • SqlBuilder

Interfaces

  • IRow
  • IRowContainer
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Database\Table;
  9: 
 10: use Nette;
 11: use Nette\Database\Reflection\MissingReferenceException;
 12: 
 13: 
 14: /**
 15:  * Single row representation.
 16:  * ActiveRow is based on the great library NotORM http://www.notorm.com written by Jakub Vrana.
 17:  *
 18:  * @author     Jakub Vrana
 19:  * @author     Jan Skrasek
 20:  */
 21: class ActiveRow implements \IteratorAggregate, IRow
 22: {
 23:     /** @var Selection */
 24:     private $table;
 25: 
 26:     /** @var array of row data */
 27:     private $data;
 28: 
 29:     /** @var bool */
 30:     private $dataRefreshed = FALSE;
 31: 
 32: 
 33:     public function __construct(array $data, Selection $table)
 34:     {
 35:         $this->data = $data;
 36:         $this->table = $table;
 37:     }
 38: 
 39: 
 40:     /**
 41:      * @internal
 42:      * @ignore
 43:      */
 44:     public function setTable(Selection $table)
 45:     {
 46:         $this->table = $table;
 47:     }
 48: 
 49: 
 50:     /**
 51:      * @internal
 52:      */
 53:     public function getTable()
 54:     {
 55:         return $this->table;
 56:     }
 57: 
 58: 
 59:     public function __toString()
 60:     {
 61:         try {
 62:             return (string) $this->getPrimary();
 63:         } catch (\Exception $e) {
 64:             if (func_num_args()) {
 65:                 throw $e;
 66:             }
 67:             trigger_error("Exception in " . __METHOD__ . "(): {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", E_USER_ERROR);
 68:         }
 69:     }
 70: 
 71: 
 72:     /**
 73:      * @return array
 74:      */
 75:     public function toArray()
 76:     {
 77:         $this->accessColumn(NULL);
 78:         return $this->data;
 79:     }
 80: 
 81: 
 82:     /**
 83:      * Returns primary key value.
 84:      * @param  bool
 85:      * @return mixed possible int, string, array, object (Nette\Utils\DateTime)
 86:      */
 87:     public function getPrimary($need = TRUE)
 88:     {
 89:         $primary = $this->table->getPrimary($need);
 90:         if ($primary === NULL) {
 91:             return NULL;
 92: 
 93:         } elseif (!is_array($primary)) {
 94:             if (isset($this->data[$primary])) {
 95:                 return $this->data[$primary];
 96:             } elseif ($need) {
 97:                 throw new Nette\InvalidStateException("Row does not contain primary $primary column data.");
 98:             } else {
 99:                 return NULL;
100:             }
101: 
102:         } else {
103:             $primaryVal = array();
104:             foreach ($primary as $key) {
105:                 if (!isset($this->data[$key])) {
106:                     if ($need) {
107:                         throw new Nette\InvalidStateException("Row does not contain primary $key column data.");
108:                     } else {
109:                         return NULL;
110:                     }
111:                 }
112:                 $primaryVal[$key] = $this->data[$key];
113:             }
114:             return $primaryVal;
115:         }
116:     }
117: 
118: 
119:     /**
120:      * Returns row signature (composition of primary keys)
121:      * @param  bool
122:      * @return string
123:      */
124:     public function getSignature($need = TRUE)
125:     {
126:         return implode('|', (array) $this->getPrimary($need));
127:     }
128: 
129: 
130:     /**
131:      * Returns referenced row.
132:      * @param  string
133:      * @param  string
134:      * @return IRow or NULL if the row does not exist
135:      */
136:     public function ref($key, $throughColumn = NULL)
137:     {
138:         if (!$throughColumn) {
139:             list($key, $throughColumn) = $this->table->getDatabaseReflection()->getBelongsToReference($this->table->getName(), $key);
140:         }
141: 
142:         return $this->getReference($key, $throughColumn);
143:     }
144: 
145: 
146:     /**
147:      * Returns referencing rows.
148:      * @param  string
149:      * @param  string
150:      * @return GroupedSelection
151:      */
152:     public function related($key, $throughColumn = NULL)
153:     {
154:         if (strpos($key, '.') !== FALSE) {
155:             list($key, $throughColumn) = explode('.', $key);
156:         } elseif (!$throughColumn) {
157:             list($key, $throughColumn) = $this->table->getDatabaseReflection()->getHasManyReference($this->table->getName(), $key);
158:         }
159: 
160:         return $this->table->getReferencingTable($key, $throughColumn, $this[$this->table->getPrimary()]);
161:     }
162: 
163: 
164:     /**
165:      * Updates row.
166:      * @param  array|\Traversable (column => value)
167:      * @return bool
168:      */
169:     public function update($data)
170:     {
171:         $selection = $this->table->createSelectionInstance()
172:             ->wherePrimary($this->getPrimary());
173: 
174:         if ($selection->update($data)) {
175:             $selection->select('*');
176:             if (($row = $selection->fetch()) === FALSE) {
177:                 throw new Nette\InvalidStateException('Database refetch failed; row does not exist!');
178:             }
179:             $this->data = $row->data;
180:             return TRUE;
181:         } else {
182:             return FALSE;
183:         }
184:     }
185: 
186: 
187:     /**
188:      * Deletes row.
189:      * @return int number of affected rows
190:      */
191:     public function delete()
192:     {
193:         $res = $this->table->createSelectionInstance()
194:             ->wherePrimary($this->getPrimary())
195:             ->delete();
196: 
197:         if ($res > 0 && ($signature = $this->getSignature(FALSE))) {
198:             unset($this->table[$signature]);
199:         }
200: 
201:         return $res;
202:     }
203: 
204: 
205:     /********************* interface IteratorAggregate ****************d*g**/
206: 
207: 
208:     public function getIterator()
209:     {
210:         $this->accessColumn(NULL);
211:         return new \ArrayIterator($this->data);
212:     }
213: 
214: 
215:     /********************* interface ArrayAccess & magic accessors ****************d*g**/
216: 
217: 
218:     /**
219:      * Stores value in column.
220:      * @param  string column name
221:      * @param  string value
222:      * @return void
223:      */
224:     public function offsetSet($key, $value)
225:     {
226:         $this->__set($key, $value);
227:     }
228: 
229: 
230:     /**
231:      * Returns value of column.
232:      * @param  string column name
233:      * @return string
234:      */
235:     public function offsetGet($key)
236:     {
237:         return $this->__get($key);
238:     }
239: 
240: 
241:     /**
242:      * Tests if column exists.
243:      * @param  string column name
244:      * @return bool
245:      */
246:     public function offsetExists($key)
247:     {
248:         return $this->__isset($key);
249:     }
250: 
251: 
252:     /**
253:      * Removes column from data.
254:      * @param  string column name
255:      * @return void
256:      */
257:     public function offsetUnset($key)
258:     {
259:         $this->__unset($key);
260:     }
261: 
262: 
263:     public function __set($key, $value)
264:     {
265:         throw new Nette\DeprecatedException('ActiveRow is read-only; use update() method instead.');
266:     }
267: 
268: 
269:     public function &__get($key)
270:     {
271:         $this->accessColumn($key);
272:         if (array_key_exists($key, $this->data)) {
273:             return $this->data[$key];
274:         }
275: 
276:         try {
277:             list($table, $column) = $this->table->getDatabaseReflection()->getBelongsToReference($this->table->getName(), $key);
278:             $referenced = $this->getReference($table, $column);
279:             if ($referenced !== FALSE) {
280:                 $this->accessColumn($key, FALSE);
281:                 return $referenced;
282:             }
283:         } catch(MissingReferenceException $e) {}
284: 
285:         $this->removeAccessColumn($key);
286:         throw new Nette\MemberAccessException("Cannot read an undeclared column '$key'.");
287:     }
288: 
289: 
290:     public function __isset($key)
291:     {
292:         $this->accessColumn($key);
293:         if (array_key_exists($key, $this->data)) {
294:             return isset($this->data[$key]);
295:         }
296:         $this->removeAccessColumn($key);
297:         return FALSE;
298:     }
299: 
300: 
301:     public function __unset($key)
302:     {
303:         throw new Nette\DeprecatedException('ActiveRow is read-only.');
304:     }
305: 
306: 
307:     protected function accessColumn($key, $selectColumn = TRUE)
308:     {
309:         $this->table->accessColumn($key, $selectColumn);
310:         if ($this->table->getDataRefreshed() && !$this->dataRefreshed) {
311:             $this->data = $this->table[$this->getSignature()]->data;
312:             $this->dataRefreshed = TRUE;
313:         }
314:     }
315: 
316: 
317:     protected function removeAccessColumn($key)
318:     {
319:         $this->table->removeAccessColumn($key);
320:     }
321: 
322: 
323:     protected function getReference($table, $column)
324:     {
325:         $this->accessColumn($column);
326:         if (array_key_exists($column, $this->data)) {
327:             $value = $this->data[$column];
328:             $referenced = $this->table->getReferencedTable($table, $column, $value);
329:             return isset($referenced[$value]) ? $referenced[$value] : NULL; // referenced row may not exist
330:         }
331: 
332:         return FALSE;
333:     }
334: 
335: }
336: 
Nette 2.2 API documentation generated by ApiGen 2.8.0