Changeset 3108
- Timestamp:
- 04/13/08 08:44:24 (3 months ago)
- Files:
-
- trunk/Solar/Sql/Select.php (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/Solar/Sql/Select.php
r3058 r3108 114 114 */ 115 115 protected $_parts = array( 116 'distinct' => false,116 'distinct' => null, 117 117 'cols' => array(), 118 118 'from' => array(), … … 226 226 * 227 227 * @param bool $flag Whether or not the SELECT is DISTINCT (default 228 * true). 228 * true). If null, the current distinct setting is not changed. 229 229 * 230 230 * @return Solar_Sql_Select … … 233 233 public function distinct($flag = true) 234 234 { 235 $this->_parts['distinct'] = (bool) $flag; 235 if ($flag !== null) { 236 $this->_parts['distinct'] = (bool) $flag; 237 } 236 238 return $this; 237 239 } … … 1172 1174 public function countPages($col = 'id') 1173 1175 { 1174 $select = clone($this); 1175 $select->clear('limit'); 1176 $select->clear('order'); 1176 // prepare the current query to become a subselect of all matching 1177 // rows; this means no limit, and no need to order them. 1178 $inner = clone($this); 1179 $inner->clear('limit'); 1180 $inner->clear('order'); 1177 1181 1178 1182 // clear all columns so there are no name conflicts 1179 // @todo Replace with $select->clear('cols') ? 1180 foreach ($select->_sources as $key => $val) { 1181 $select->_sources[$key]['cols'] = array(); 1182 } 1183 1184 // add a single COUNT() column 1185 $select->_addSource( 1183 foreach ($inner->_sources as $key => $val) { 1184 $inner->_sources[$key]['cols'] = array(); 1185 } 1186 1187 // add the one column we're counting on 1188 $inner->_addSource( 1186 1189 'cols', // type 1187 1190 null, // name … … 1189 1192 null, // join 1190 1193 null, // cond 1191 "COUNT($col)"1194 $col 1192 1195 ); 1193 1196 1197 // does the counting column have a dot in it? 1198 $pos = strpos($col, '.'); 1199 if ($pos) { 1200 // alias the subselect to the same table name as the column 1201 $alias = substr($col, 0, $pos); 1202 $col = substr($col, $pos + 1); 1203 } else { 1204 // default alias 'subselect' in lieu of an explicit one 1205 $alias = 'subselect'; 1206 } 1207 1208 // build the outer select, which will do the actual count. 1209 // wrapping with an outer select lets us have all manner of weirdness 1210 // in the inner query, so that it doesn't conflict with the count. 1211 $outer = clone($this); 1212 $outer->clear(); 1213 $outer->fromSelect($inner, $alias, "COUNT($alias.$col)"); 1214 1194 1215 // get the count and calculate pages 1195 $count = $ select->fetchValue();1216 $count = $outer->fetchValue(); 1196 1217 $pages = 0; 1197 1218 if ($count > 0) {
