Namespaces

  • 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

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