Packages

  • Nette
    • Application
      • Application\Diagnostics
      • Application\Responses
      • Application\Routers
      • Application\UI
    • Caching
      • Caching\Storages
    • ComponentModel
    • Config
    • Database
      • Database\Diagnostics
      • Database\Drivers
      • Database\Reflection
      • Database\Table
    • DI
    • Diagnostics
    • Forms
      • Forms\Controls
      • Forms\Rendering
    • Http
    • Iterators
    • Latte
      • Latte\Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • None
  • PHP

Classes

  • GroupedTableSelection
  • TableRow
  • TableSelection
  • 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, 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:  * @package Nette\Database\Table
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * Single row representation.
 17:  * Selector 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 TableRow extends Object implements IteratorAggregate, ArrayAccess
 23: {
 24:     /** @var TableSelection */
 25:     protected $table;
 26: 
 27:     /** @var array of row data */
 28:     protected $data;
 29: 
 30:     /** @var array of new values {@see TableRow::update()} */
 31:     private $modified = array();
 32: 
 33: 
 34: 
 35:     public function __construct(array $data, TableSelection $table)
 36:     {
 37:         $this->data = $data;
 38:         $this->table = $table;
 39:     }
 40: 
 41: 
 42: 
 43:     /**
 44:      * Returns primary key value.
 45:      * @return string
 46:      */
 47:     public function __toString()
 48:     {
 49:         return (string) $this[$this->table->primary]; // (string) - PostgreSQL returns int
 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 referenced row.
 67:      * @param  string
 68:      * @return TableRow or NULL if the row does not exist
 69:      */
 70:     public function ref($name)
 71:     {
 72:         $referenced = $this->table->getReferencedTable($name, $column);
 73:         if (isset($referenced[$this[$column]])) { // referenced row may not exist
 74:             $res = $referenced[$this[$column]];
 75:             return $res;
 76:         }
 77:     }
 78: 
 79: 
 80: 
 81:     /**
 82:      * Returns referencing rows.
 83:      * @param  string table name
 84:      * @return GroupedTableSelection
 85:      */
 86:     public function related($table)
 87:     {
 88:         $referencing = $this->table->getReferencingTable($table);
 89:         $referencing->active = $this[$this->table->primary];
 90:         return $referencing;
 91:     }
 92: 
 93: 
 94: 
 95:     /**
 96:      * Updates row.
 97:      * @param  array or NULL for all modified values
 98:      * @return int number of affected rows or FALSE in case of an error
 99:      */
100:     public function update($data = NULL)
101:     {
102:         if ($data === NULL) {
103:             $data = $this->modified;
104:         }
105:         return $this->table->connection->table($this->table->name)
106:             ->where($this->table->primary, $this[$this->table->primary])
107:             ->update($data);
108:     }
109: 
110: 
111: 
112:     /**
113:      * Deletes row.
114:      * @return int number of affected rows or FALSE in case of an error
115:      */
116:     public function delete()
117:     {
118:         return $this->table->connection->table($this->table->name)
119:             ->where($this->table->primary, $this[$this->table->primary])
120:             ->delete();
121:     }
122: 
123: 
124: 
125:     /********************* interface IteratorAggregate ****************d*g**/
126: 
127: 
128: 
129:     public function getIterator()
130:     {
131:         $this->access(NULL);
132:         return new ArrayIterator($this->data);
133:     }
134: 
135: 
136: 
137:     /********************* interface ArrayAccess & magic accessors ****************d*g**/
138: 
139: 
140: 
141:     /**
142:      * Stores value in column.
143:      * @param  string column name
144:      * @return NULL
145:      */
146:     public function offsetSet($key, $value)
147:     {
148:         $this->__set($key, $value);
149:     }
150: 
151: 
152: 
153:     /**
154:      * Returns value of column.
155:      * @param  string column name
156:      * @return string
157:      */
158:     public function offsetGet($key)
159:     {
160:         return $this->__get($key);
161:     }
162: 
163: 
164: 
165:     /**
166:      * Tests if column exists.
167:      * @param  string column name
168:      * @return bool
169:      */
170:     public function offsetExists($key)
171:     {
172:         return $this->__isset($key);
173:     }
174: 
175: 
176: 
177:     /**
178:      * Removes column from data.
179:      * @param  string column name
180:      * @return NULL
181:      */
182:     public function offsetUnset($key)
183:     {
184:         $this->__unset($key);
185:     }
186: 
187: 
188: 
189:     public function __set($key, $value)
190:     {
191:         $this->data[$key] = $value;
192:         $this->modified[$key] = $value;
193:     }
194: 
195: 
196: 
197:     public function &__get($key)
198:     {
199:         if (array_key_exists($key, $this->data)) {
200:             $this->access($key);
201:             return $this->data[$key];
202:         }
203: 
204:         $column = $this->table->connection->databaseReflection->getReferencedColumn($key, $this->table->name);
205:         if (array_key_exists($column, $this->data)) {
206:             $value = $this->data[$column];
207:             $referenced = $this->table->getReferencedTable($key);
208:             $ret = isset($referenced[$value]) ? $referenced[$value] : NULL; // referenced row may not exist
209:             return $ret;
210:         }
211: 
212:         $this->access($key);
213:         if (array_key_exists($key, $this->data)) {
214:             return $this->data[$key];
215: 
216:         } else {
217:             $this->access($key, TRUE);
218: 
219:             $this->access($column);
220:             if (array_key_exists($column, $this->data)) {
221:                 $value = $this->data[$column];
222:                 $referenced = $this->table->getReferencedTable($key);
223:                 $ret = isset($referenced[$value]) ? $referenced[$value] : NULL; // referenced row may not exist
224: 
225:             } else {
226:                 $this->access($column, TRUE);
227:                 trigger_error("Unknown column $key", E_USER_WARNING);
228:                 $ret = NULL;
229:             }
230:             return $ret;
231:         }
232:     }
233: 
234: 
235: 
236:     public function __isset($key)
237:     {
238:         $this->access($key);
239:         $return = array_key_exists($key, $this->data);
240:         if (!$return) {
241:             $this->access($key, TRUE);
242:         }
243:         return $return;
244:     }
245: 
246: 
247: 
248:     public function __unset($key)
249:     {
250:         unset($this->data[$key]);
251:         unset($this->modified[$key]);
252:     }
253: 
254: 
255: 
256:     public function access($key, $delete = FALSE)
257:     {
258:         if ($this->table->connection->getCache() && !isset($this->modified[$key]) && $this->table->access($key, $delete)) {
259:             $id = (isset($this->data[$this->table->primary]) ? $this->data[$this->table->primary] : $this->data);
260:             $this->data = $this->table[$id]->data;
261:         }
262:     }
263: 
264: }
265: 
Nette Framework 2.0beta1 (for PHP 5.2) API API documentation generated by ApiGen 2.3.0