Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
    • Templating
    • Utils
  • 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, 2011 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:  * Selector 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:     protected $table;
 28: 
 29:     /** @var array of row data */
 30:     protected $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:      * Returns primary key value.
 47:      * @return string
 48:      */
 49:     public function __toString()
 50:     {
 51:         return (string) $this[$this->table->primary]; // (string) - PostgreSQL returns int
 52:     }
 53: 
 54: 
 55: 
 56:     /**
 57:      * @return array
 58:      */
 59:     public function toArray()
 60:     {
 61:         $this->access(NULL);
 62:         return $this->data;
 63:     }
 64: 
 65: 
 66: 
 67:     /**
 68:      * Returns referenced row.
 69:      * @param  string
 70:      * @return ActiveRow or NULL if the row does not exist
 71:      */
 72:     public function ref($name)
 73:     {
 74:         $referenced = $this->table->getReferencedTable($name, $column);
 75:         if (isset($referenced[$this[$column]])) { // referenced row may not exist
 76:             $res = $referenced[$this[$column]];
 77:             return $res;
 78:         }
 79:     }
 80: 
 81: 
 82: 
 83:     /**
 84:      * Returns referencing rows.
 85:      * @param  string table name
 86:      * @return GroupedSelection
 87:      */
 88:     public function related($table)
 89:     {
 90:         $referencing = $this->table->getReferencingTable($table);
 91:         $referencing->active = $this[$this->table->primary];
 92:         return $referencing;
 93:     }
 94: 
 95: 
 96: 
 97:     /**
 98:      * Updates row.
 99:      * @param  array or NULL for all modified values
100:      * @return int number of affected rows or FALSE in case of an error
101:      */
102:     public function update($data = NULL)
103:     {
104:         if ($data === NULL) {
105:             $data = $this->modified;
106:         }
107:         return $this->table->connection->table($this->table->name)
108:             ->where($this->table->primary, $this[$this->table->primary])
109:             ->update($data);
110:     }
111: 
112: 
113: 
114:     /**
115:      * Deletes row.
116:      * @return int number of affected rows or FALSE in case of an error
117:      */
118:     public function delete()
119:     {
120:         return $this->table->connection->table($this->table->name)
121:             ->where($this->table->primary, $this[$this->table->primary])
122:             ->delete();
123:     }
124: 
125: 
126: 
127:     /********************* interface IteratorAggregate ****************d*g**/
128: 
129: 
130: 
131:     public function getIterator()
132:     {
133:         $this->access(NULL);
134:         return new \ArrayIterator($this->data);
135:     }
136: 
137: 
138: 
139:     /********************* interface ArrayAccess & magic accessors ****************d*g**/
140: 
141: 
142: 
143:     /**
144:      * Stores value in column.
145:      * @param  string column name
146:      * @return NULL
147:      */
148:     public function offsetSet($key, $value)
149:     {
150:         $this->__set($key, $value);
151:     }
152: 
153: 
154: 
155:     /**
156:      * Returns value of column.
157:      * @param  string column name
158:      * @return string
159:      */
160:     public function offsetGet($key)
161:     {
162:         return $this->__get($key);
163:     }
164: 
165: 
166: 
167:     /**
168:      * Tests if column exists.
169:      * @param  string column name
170:      * @return bool
171:      */
172:     public function offsetExists($key)
173:     {
174:         return $this->__isset($key);
175:     }
176: 
177: 
178: 
179:     /**
180:      * Removes column from data.
181:      * @param  string column name
182:      * @return NULL
183:      */
184:     public function offsetUnset($key)
185:     {
186:         $this->__unset($key);
187:     }
188: 
189: 
190: 
191:     public function __set($key, $value)
192:     {
193:         $this->data[$key] = $value;
194:         $this->modified[$key] = $value;
195:     }
196: 
197: 
198: 
199:     public function &__get($key)
200:     {
201:         if (array_key_exists($key, $this->data)) {
202:             $this->access($key);
203:             return $this->data[$key];
204:         }
205: 
206:         $column = $this->table->connection->databaseReflection->getReferencedColumn($key, $this->table->name);
207:         if (array_key_exists($column, $this->data)) {
208:             $value = $this->data[$column];
209:             $referenced = $this->table->getReferencedTable($key);
210:             $ret = isset($referenced[$value]) ? $referenced[$value] : NULL; // referenced row may not exist
211:             return $ret;
212:         }
213: 
214:         $this->access($key);
215:         if (array_key_exists($key, $this->data)) {
216:             return $this->data[$key];
217: 
218:         } else {
219:             $this->access($key, TRUE);
220: 
221:             $this->access($column);
222:             if (array_key_exists($column, $this->data)) {
223:                 $value = $this->data[$column];
224:                 $referenced = $this->table->getReferencedTable($key);
225:                 $ret = isset($referenced[$value]) ? $referenced[$value] : NULL; // referenced row may not exist
226: 
227:             } else {
228:                 $this->access($column, TRUE);
229:                 trigger_error("Unknown column $key", E_USER_WARNING);
230:                 $ret = NULL;
231:             }
232:             return $ret;
233:         }
234:     }
235: 
236: 
237: 
238:     public function __isset($key)
239:     {
240:         $this->access($key);
241:         $return = array_key_exists($key, $this->data);
242:         if (!$return) {
243:             $this->access($key, TRUE);
244:         }
245:         return $return;
246:     }
247: 
248: 
249: 
250:     public function __unset($key)
251:     {
252:         unset($this->data[$key]);
253:         unset($this->modified[$key]);
254:     }
255: 
256: 
257: 
258:     public function access($key, $delete = FALSE)
259:     {
260:         if ($this->table->connection->getCache() && !isset($this->modified[$key]) && $this->table->access($key, $delete)) {
261:             $id = (isset($this->data[$this->table->primary]) ? $this->data[$this->table->primary] : $this->data);
262:             $this->data = $this->table[$id]->data;
263:         }
264:     }
265: 
266: }
267: 
Nette Framework 2.0beta1 API API documentation generated by ApiGen 2.3.0