Changeset 4684

Show
Ignore:
Timestamp:
07/13/08 01:54:30 (5 months ago)
Author:
jwage
Message:

fixes #1195

Location:
branches/1.0
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • branches/1.0/lib/Doctrine/RawSql.php

    r4572 r4684  
    7373            return $this; 
    7474        } 
    75         if ( ! isset($this->parts[$queryPartName])) { 
     75        if ( ! isset($this->_sqlParts[$queryPartName])) { 
    7676            $this->_sqlParts[$queryPartName] = array(); 
    7777        } 
     
    221221        } 
    222222         
     223        $q = 'SELECT '; 
     224        if($this->_sqlParts['distinct'] == true) $q .= 'DISTINCT '; 
     225 
    223226        // first add the fields of the root component 
    224227        reset($this->_queryComponents); 
    225228        $componentAlias = key($this->_queryComponents); 
    226  
    227         $q = 'SELECT ' . implode(', ', $select[$componentAlias]); 
     229        $q .= implode(', ', $select[$componentAlias]); 
    228230        unset($select[$componentAlias]); 
    229231 
     
    238240            $this->_sqlParts['where'][] = $string; 
    239241        } 
    240         $copy = $this->_sqlParts; 
    241         unset($copy['select']); 
    242242 
    243243        $q .= ( ! empty($this->_sqlParts['from']))?    ' FROM '     . implode(' ', $this->_sqlParts['from']) : ''; 
     
    253253        } 
    254254        return $q; 
     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; 
    255334    } 
    256335 
  • branches/1.0/tests/run.php

    r4657 r4684  
    8787$tickets->addTestCase(new Doctrine_Ticket_1175_TestCase()); 
    8888$tickets->addTestCase(new Doctrine_Ticket_1192_TestCase()); 
     89$tickets->addTestCase(new Doctrine_Ticket_1195_TestCase()); 
    8990$tickets->addTestCase(new Doctrine_Ticket_1205_TestCase()); 
    9091$tickets->addTestCase(new Doctrine_Ticket_1206_TestCase());