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