Doctrine 1
  1. Doctrine 1
  2. DC-567

RawSql: getCountSqlQuery does not work correctly with PostgreSQL (patch provided)

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.2.1
    • Fix Version/s: 1.2.2
    • Component/s: Query
    • Labels:
      None

      Description

      Dears,

      Line 309 in Doctrine_RawSql:

              $q = 'SELECT COUNT( DISTINCT '.implode(',',$fields).') as num_results';
      
              $string = $this->getInheritanceCondition($this->getRootAlias());
              if ( ! empty($string)) {
                  $this->_sqlParts['where'][] = $string;
              }
      
              $q .= ( ! empty($this->_sqlParts['from']))?    ' FROM '     . implode(' ', $this->_sqlParts['from']) : '';
              $q .= ( ! empty($this->_sqlParts['where']))?   ' WHERE '    . implode(' AND ', $this->_sqlParts['where']) : '';
              $q .= ( ! empty($this->_sqlParts['groupby']))? ' GROUP BY ' . implode(', ', $this->_sqlParts['groupby']) : '';
              $q .= ( ! empty($this->_sqlParts['having']))?  ' HAVING '   . implode(' AND ', $this->_sqlParts['having']) : '';
      

      produces with PostgreSQL: Undefined function: 7 ERROR: function count(bigint, integer) does not exist

      but

              $q = 'SELECT COUNT(*) as num_results FROM (SELECT DISTINCT '.implode(', ',$fields);
      
              $string = $this->getInheritanceCondition($this->getRootAlias());
              if ( ! empty($string)) {
                  $this->_sqlParts['where'][] = $string;
              }
      
              $q .= ( ! empty($this->_sqlParts['from']))?    ' FROM '     . implode(' ', $this->_sqlParts['from']) : '';
              $q .= ( ! empty($this->_sqlParts['where']))?   ' WHERE '    . implode(' AND ', $this->_sqlParts['where']) : '';
              $q .= ( ! empty($this->_sqlParts['groupby']))? ' GROUP BY ' . implode(', ', $this->_sqlParts['groupby']) : '';
              $q .= ( ! empty($this->_sqlParts['having']))?  ' HAVING '   . implode(' AND ', $this->_sqlParts['having']) : '';
      
              $q .= ') as results';
      

      works and should work with every database server.

      Continue your great work

      Regards,

      Raphaël Dehousse

        Activity

        Hide
        Jonathan H. Wage added a comment -

        Hi, it would be really helpful if all suggested changes are supplied as a patch.

        Show
        Jonathan H. Wage added a comment - Hi, it would be really helpful if all suggested changes are supplied as a patch.

          People

          • Assignee:
            Jonathan H. Wage
            Reporter:
            Raphaël Dehousse
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: