Changeset 3928

Show
Ignore:
Timestamp:
03/04/08 23:55:55 (16 months ago)
Author:
jwage
Message:

fixes #451

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/0.10/lib/Doctrine/Query.php

    r3913 r3928  
    17651765        $map = reset($this->_queryComponents); 
    17661766        $componentAlias = key($this->_queryComponents); 
     1767        $tableAlias = $this->getTableAlias($componentAlias); 
    17671768        $table = $map['table']; 
     1769        $idColumnNames = $table->getIdentifierColumnNames(); 
    17681770 
    17691771        // build the query base 
    1770         $q  = 'SELECT COUNT(DISTINCT ' . $this->getTableAlias($componentAlias) 
    1771               . '.' . implode(',', $table->getIdentifierColumnNames()) 
     1772        $q  = 'SELECT COUNT(DISTINCT ' . $tableAlias 
     1773              . '.' . implode(', ' . $tableAlias . '.', $idColumnNames) 
    17721774              . ') AS num_results'; 
    17731775 
     
    17861788            $where[] = $string; 
    17871789        } 
     1790 
    17881791        // append conditions 
    17891792        $q .= ( ! empty($where)) ?  ' WHERE '  . implode(' AND ', $where) : ''; 
    1790         $q .= ( ! empty($groupby)) ?  ' GROUP BY '  . implode(', ', $groupby) : ''; 
     1793 
     1794        if ( ! empty($groupby)) { 
     1795          // Maintain existing groupby 
     1796          $q .= ' GROUP BY '  . implode(', ', $groupby); 
     1797        } else { 
     1798          // Default groupby to primary identifier. Database defaults to this internally 
     1799          // This is required for situations where the user has aggregate functions in the select part 
     1800          // Without the groupby it fails 
     1801          $q .= ' GROUP BY ' . $tableAlias . '.' . implode(', ' . $tableAlias . '.', $idColumnNames); 
     1802        } 
     1803 
    17911804        $q .= ( ! empty($having)) ? ' HAVING ' . implode(' AND ', $having): ''; 
    17921805 
     
    17941807            $params = array($params); 
    17951808        } 
     1809   
    17961810        // append parameters 
    17971811        $params = array_merge($this->_params['where'], $this->_params['having'], $this->_params['join'], $params);