Packages

  • 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

  • NGroupedTableSelection
  • NSqlBuilder
  • NTableRow
  • NTableSelection
  • 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 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:  * Representation of filtered table grouped by some column.
 17:  * GroupedSelection is based on the great library NotORM http://www.notorm.com written by Jakub Vrana.
 18:  *
 19:  * @author     Jakub Vrana
 20:  * @author     Jan Skrasek
 21:  * @package Nette\Database\Table
 22:  */
 23: class NGroupedTableSelection extends NTableSelection
 24: {
 25:     /** @var NTableSelection referenced table */
 26:     protected $refTable;
 27: 
 28:     /** @var string grouping column name */
 29:     protected $column;
 30: 
 31:     /** @var int primary key */
 32:     protected $active;
 33: 
 34: 
 35: 
 36:     /**
 37:      * Creates filtered and grouped table representation.
 38:      * @param  NTableSelection  $refTable
 39:      * @param  string  database table name
 40:      * @param  string  joining column
 41:      */
 42:     public function __construct(NTableSelection $refTable, $table, $column)
 43:     {
 44:         parent::__construct($table, $refTable->connection);
 45:         $this->refTable = $refTable;
 46:         $this->column = $column;
 47:     }
 48: 
 49: 
 50: 
 51:     /**
 52:      * Sets active group.
 53:      * @internal
 54:      * @param  int  primary key of grouped rows
 55:      * @return NGroupedTableSelection
 56:      */
 57:     public function setActive($active)
 58:     {
 59:         $this->active = $active;
 60:         return $this;
 61:     }
 62: 
 63: 
 64: 
 65:     /** @deprecated */
 66:     public function through($column)
 67:     {
 68:         trigger_error(__METHOD__ . '() is deprecated; use ' . __CLASS__ . '::related("' . $this->name . '", "' . $column . '") instead.', E_USER_WARNING);
 69:         $this->column = $column;
 70:         $this->delimitedColumn = $this->refTable->connection->getSupplementalDriver()->delimite($this->column);
 71:         return $this;
 72:     }
 73: 
 74: 
 75: 
 76:     public function select($columns)
 77:     {
 78:         if (!$this->sqlBuilder->getSelect()) {
 79:             $this->sqlBuilder->addSelect("$this->name.$this->column");
 80:         }
 81: 
 82:         return parent::select($columns);
 83:     }
 84: 
 85: 
 86: 
 87:     public function order($columns)
 88:     {
 89:         if (!$this->sqlBuilder->getOrder()) {
 90:             // improve index utilization
 91:             $this->sqlBuilder->addOrder("$this->name.$this->column" . (preg_match('~\\bDESC$~i', $columns) ? ' DESC' : ''));
 92:         }
 93: 
 94:         return parent::order($columns);
 95:     }
 96: 
 97: 
 98: 
 99:     /********************* aggregations ****************d*g**/
100: 
101: 
102: 
103:     public function aggregation($function)
104:     {
105:         $aggregation = & $this->getRefTable($refPath)->aggregation[$refPath . $function . $this->sqlBuilder->buildSelectQuery() . json_encode($this->sqlBuilder->getParameters())];
106: 
107:         if ($aggregation === NULL) {
108:             $aggregation = array();
109: 
110:             $selection = $this->createSelectionInstance();
111:             $selection->getSqlBuilder()->importConditions($this->getSqlBuilder());
112:             $selection->select($function);
113:             $selection->select("$this->name.$this->column");
114:             $selection->group("$this->name.$this->column");
115: 
116:             foreach ($selection as $row) {
117:                 $aggregation[$row[$this->column]] = $row;
118:             }
119:         }
120: 
121:         if (isset($aggregation[$this->active])) {
122:             foreach ($aggregation[$this->active] as $val) {
123:                 return $val;
124:             }
125:         }
126:     }
127: 
128: 
129: 
130:     public function count($column = NULL)
131:     {
132:         $return = parent::count($column);
133:         return isset($return) ? $return : 0;
134:     }
135: 
136: 
137: 
138:     /********************* internal ****************d*g**/
139: 
140: 
141: 
142:     protected function execute()
143:     {
144:         if ($this->rows !== NULL) {
145:             return;
146:         }
147: 
148:         $hash = md5($this->sqlBuilder->buildSelectQuery() . json_encode($this->sqlBuilder->getParameters()));
149: 
150:         $referencing = & $this->getRefTable($refPath)->referencing[$refPath . $hash];
151:         $this->rows = & $referencing['rows'];
152:         $this->referenced = & $referencing['refs'];
153:         $this->accessed = & $referencing['accessed'];
154:         $refData = & $referencing['data'];
155: 
156:         if ($refData === NULL) {
157:             $limit = $this->sqlBuilder->getLimit();
158:             $rows = count($this->refTable->rows);
159:             if ($limit && $rows > 1) {
160:                 $this->sqlBuilder->setLimit(NULL, NULL);
161:             }
162:             parent::execute();
163:             $this->sqlBuilder->setLimit($limit, NULL);
164:             $refData = array();
165:             $offset = array();
166:             foreach ($this->rows as $key => $row) {
167:                 $ref = & $refData[$row[$this->column]];
168:                 $skip = & $offset[$row[$this->column]];
169:                 if ($limit === NULL || $rows <= 1 || (count($ref) < $limit && $skip >= $this->sqlBuilder->getOffset())) {
170:                     $ref[$key] = $row;
171:                 } else {
172:                     unset($this->rows[$key]);
173:                 }
174:                 $skip++;
175:                 unset($ref, $skip);
176:             }
177:         }
178: 
179:         $this->data = & $refData[$this->active];
180:         if ($this->data === NULL) {
181:             $this->data = array();
182:         } else {
183:             foreach ($this->data as $row) {
184:                 $row->setTable($this); // injects correct parent GroupedSelection
185:             }
186:             reset($this->data);
187:         }
188:     }
189: 
190: 
191: 
192:     protected function getRefTable(& $refPath)
193:     {
194:         $refObj = $this->refTable;
195:         $refPath = $this->name . '.';
196:         while ($refObj instanceof NGroupedTableSelection) {
197:             $refPath .= $refObj->name . '.';
198:             $refObj = $refObj->refTable;
199:         }
200: 
201:         return $refObj;
202:     }
203: 
204: 
205: 
206:     /********************* manipulation ****************d*g**/
207: 
208: 
209: 
210:     public function insert($data)
211:     {
212:         if ($data instanceof Traversable && !$data instanceof NTableSelection) {
213:             $data = iterator_to_array($data);
214:         }
215: 
216:         if (NValidators::isList($data)) {
217:             foreach (array_keys($data) as $key) {
218:                 $data[$key][$this->column] = $this->active;
219:             }
220:         } else {
221:             $data[$this->column] = $this->active;
222:         }
223: 
224:         return parent::insert($data);
225:     }
226: 
227: 
228: 
229:     public function update($data)
230:     {
231:         $builder = $this->sqlBuilder;
232: 
233:         $this->sqlBuilder = new NSqlBuilder($this);
234:         $this->where($this->column, $this->active);
235:         $return = parent::update($data);
236: 
237:         $this->sqlBuilder = $builder;
238:         return $return;
239:     }
240: 
241: 
242: 
243:     public function delete()
244:     {
245:         $builder = $this->sqlBuilder;
246: 
247:         $this->sqlBuilder = new NSqlBuilder($this);
248:         $this->where($this->column, $this->active);
249:         $return = parent::delete();
250: 
251:         $this->sqlBuilder = $builder;
252:         return $return;
253:     }
254: 
255: }
256: 
Nette Framework 2.0.6 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.7.0