| | 255 | } |
| | 256 | |
| | 257 | /** |
| | 258 | * getCountQuery |
| | 259 | * builds the count query. |
| | 260 | * |
| | 261 | * @return string the built sql query |
| | 262 | */ |
| | 263 | public function getCountQuery($params = array()) |
| | 264 | { |
| | 265 | //Doing COUNT( DISTINCT rootComponent.id ) |
| | 266 | //This is not correct, if the result is not hydrated by doctrine, but it mimics the behaviour of Doctrine_Query::getCountQuery |
| | 267 | reset($this->_queryComponents); |
| | 268 | $componentAlias = key($this->_queryComponents); |
| | 269 | $tableAlias = $this->getSqlTableAlias($componentAlias); |
| | 270 | $fields = array(); |
| | 271 | |
| | 272 | foreach ((array) $this->_queryComponents[$componentAlias]['table']->getIdentifierColumnNames() as $key) { |
| | 273 | $fields[] = $tableAlias . '.' . $key; |
| | 274 | } |
| | 275 | |
| | 276 | $q = 'SELECT COUNT( DISTINCT '.implode(',',$fields).') as num_results'; |
| | 277 | |
| | 278 | $string = $this->getInheritanceCondition($this->getRootAlias()); |
| | 279 | if ( ! empty($string)) { |
| | 280 | $this->_sqlParts['where'][] = $string; |
| | 281 | } |
| | 282 | |
| | 283 | $q .= ( ! empty($this->_sqlParts['from']))? ' FROM ' . implode(' ', $this->_sqlParts['from']) : ''; |
| | 284 | $q .= ( ! empty($this->_sqlParts['where']))? ' WHERE ' . implode(' AND ', $this->_sqlParts['where']) : ''; |
| | 285 | $q .= ( ! empty($this->_sqlParts['groupby']))? ' GROUP BY ' . implode(', ', $this->_sqlParts['groupby']) : ''; |
| | 286 | $q .= ( ! empty($this->_sqlParts['having']))? ' HAVING ' . implode(' AND ', $this->_sqlParts['having']) : ''; |
| | 287 | |
| | 288 | if ( ! empty($string)) { |
| | 289 | array_pop($this->_sqlParts['where']); |
| | 290 | } |
| | 291 | |
| | 292 | return $q; |
| | 293 | } |
| | 294 | |
| | 295 | /** |
| | 296 | * count |
| | 297 | * fetches the count of the query |
| | 298 | * |
| | 299 | * This method executes the main query without all the |
| | 300 | * selected fields, ORDER BY part, LIMIT part and OFFSET part. |
| | 301 | * |
| | 302 | * This is an exact copy of the Dql Version |
| | 303 | * |
| | 304 | * @see Doctrine_Query::count() |
| | 305 | * @param array $params an array of prepared statement parameters |
| | 306 | * @return integer the count of this query |
| | 307 | */ |
| | 308 | public function count($params = array()) |
| | 309 | { |
| | 310 | $q = $this->getCountQuery(); |
| | 311 | |
| | 312 | if ( ! is_array($params)) { |
| | 313 | $params = array($params); |
| | 314 | } |
| | 315 | |
| | 316 | $params = array_merge($this->_params['join'], $this->_params['where'], $this->_params['having'], $params); |
| | 317 | |
| | 318 | $params = $this->convertEnums($params); |
| | 319 | |
| | 320 | $results = $this->getConnection()->fetchAll($q, $params); |
| | 321 | |
| | 322 | if (count($results) > 1) { |
| | 323 | $count = count($results); |
| | 324 | } else { |
| | 325 | if (isset($results[0])) { |
| | 326 | $results[0] = array_change_key_case($results[0], CASE_LOWER); |
| | 327 | $count = $results[0]['num_results']; |
| | 328 | } else { |
| | 329 | $count = 0; |
| | 330 | } |
| | 331 | } |
| | 332 | |
| | 333 | return (int) $count; |