Changeset 3538

Show
Ignore:
Timestamp:
01/17/08 14:37:11 (18 months ago)
Author:
guilhermeblanco
Message:

Added different counter query support for Doctrine_Pager. Now it can use 2 different queries to paginate items. Added 4 new methods: setCountQuery, getCountQuery, setCountQueryParams and getCountQueryParams. If not defined a counter Doctrine_Query, the collector one is used. If no params were defined to be used in counter query, the ones passed by execute method are used

Files:
3 modified

Legend:

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

    r3529 r3538  
    4040 
    4141    /** 
     42     * @var Doctrine_Query $_countQuery Doctrine_Query object related to the counter of pager 
     43     */ 
     44    protected $_countQuery; 
     45 
     46    /** 
     47     * @var array $_countQueryParams    Hold the params to be used by Doctrine_Query counter object of pager 
     48     */ 
     49    protected $_countQueryParams; 
     50 
     51    /** 
    4252     * @var integer $_numResults        Number of results found 
    4353     */ 
     
    97107    { 
    98108        // retrieve the number of items found 
    99         $count = $this->getQuery()->count($params); 
     109        $count = $this->getCountQuery()->count($this->getCountQueryParams($params)); 
    100110        $this->_setNumResults($count); 
    101111 
     
    378388     * getQuery 
    379389     * 
    380      * Returns the Doctrine_Query object related to the pager 
    381      * 
    382      * @return Doctrine_Query        Doctrine_Query object related to the pager 
     390     * Returns the Doctrine_Query collector object related to the pager 
     391     * 
     392     * @return Doctrine_Query    Doctrine_Query object related to the pager 
    383393     */ 
    384394    public function getQuery() 
     
    391401     * _setQuery 
    392402     * 
    393      * Defines the maximum number of itens per page 
    394      * 
    395      * @param $query     Accepts either a Doctrine_Query object or a string  
    396      *                   (which does the Doctrine_Query class creation). 
     403     * Defines the collector query to be used by pager 
     404     * 
     405     * @param Doctrine_Query     Accepts either a Doctrine_Query object or a string  
     406     *                           (which does the Doctrine_Query class creation). 
    397407     * @return void 
    398408     */ 
     
    404414 
    405415        $this->_query = $query; 
     416    } 
     417 
     418 
     419    /** 
     420     * getCountQuery 
     421     * 
     422     * Returns the Doctrine_Query object that is used to make the count results to pager 
     423     * 
     424     * @return Doctrine_Query     Doctrine_Query object related to the pager 
     425     */ 
     426    public function getCountQuery() 
     427    { 
     428        return ($this->_countQuery !== null) ? $this->_countQuery : $this->_query; 
     429    } 
     430 
     431 
     432    /** 
     433     * setCountQuery 
     434     * 
     435     * Defines the counter query to be used by pager 
     436     * 
     437     * @param Doctrine_Query     Accepts either a Doctrine_Query object or a string  
     438     *                           (which does the Doctrine_Query class creation). 
     439     * @return void 
     440     */ 
     441    public function setCountQuery($query) 
     442    { 
     443        if (is_string($query)) { 
     444            $query = Doctrine_Query::create()->parseQuery($query); 
     445        } 
     446 
     447        $this->_countQuery = $query; 
     448 
     449        $this->_setExecuted(false); 
     450    } 
     451 
     452 
     453    /** 
     454     * getCountQueryParams 
     455     * 
     456     * Returns the params to be used by counter Doctrine_Query 
     457     * 
     458     * @return array     Doctrine_Query counter params 
     459     */ 
     460    public function getCountQueryParams($defaultParams = array()) 
     461    { 
     462        return ($this->_countQueryParams !== null) ? $this->_countQueryParams : $defaultParams; 
     463    } 
     464 
     465 
     466    /** 
     467     * setCountQueryParams 
     468     * 
     469     * Defines the params to be used by counter Doctrine_Query 
     470     * 
     471     * @param array       Optional params to be used by counter Doctrine_Query.  
     472     *                    If not defined, the params passed to execute method will be used. 
     473     * @param boolean     Optional argument that append the query param instead of overriding the existent ones. 
     474     * @return void 
     475     */ 
     476    public function setCountQueryParams($params = array(), $append = false) 
     477    { 
     478        if ($append && is_array($this->_countQueryParams)) { 
     479            $this->_countQueryParams = array_merge($this->_countQueryParams, $params); 
     480        } else { 
     481            $this->_countQueryParams = $params; 
     482        } 
     483 
     484        $this->_setExecuted(false); 
    406485    } 
    407486 
     
    419498    public function execute($params = array(), $hydrationMode = Doctrine::FETCH_RECORD) 
    420499    { 
    421         $this->_initialize($params); 
     500        if (!$this->getExecuted()) { 
     501            $this->_initialize($params); 
     502        } 
    422503 
    423504        return $this->getQuery()->execute($params, $hydrationMode); 
  • branches/0.9/lib/Doctrine/Pager.php

    r3529 r3538  
    4040 
    4141    /** 
     42     * @var Doctrine_Query $_countQuery Doctrine_Query object related to the counter of pager 
     43     */ 
     44    protected $_countQuery; 
     45 
     46    /** 
     47     * @var array $_countQueryParams    Hold the params to be used by Doctrine_Query counter object of pager 
     48     */ 
     49    protected $_countQueryParams; 
     50 
     51    /** 
    4252     * @var integer $_numResults        Number of results found 
    4353     */ 
     
    97107    { 
    98108        // retrieve the number of items found 
    99         $count = $this->getQuery()->count($params); 
     109        $count = $this->getCountQuery()->count($this->getCountQueryParams($params)); 
    100110        $this->_setNumResults($count); 
    101111 
     
    378388     * getQuery 
    379389     * 
    380      * Returns the Doctrine_Query object related to the pager 
    381      * 
    382      * @return Doctrine_Query        Doctrine_Query object related to the pager 
     390     * Returns the Doctrine_Query collector object related to the pager 
     391     * 
     392     * @return Doctrine_Query    Doctrine_Query object related to the pager 
    383393     */ 
    384394    public function getQuery() 
     
    391401     * _setQuery 
    392402     * 
    393      * Defines the maximum number of itens per page 
    394      * 
    395      * @param $query     Accepts either a Doctrine_Query object or a string  
    396      *                   (which does the Doctrine_Query class creation). 
     403     * Defines the collector query to be used by pager 
     404     * 
     405     * @param Doctrine_Query     Accepts either a Doctrine_Query object or a string  
     406     *                           (which does the Doctrine_Query class creation). 
    397407     * @return void 
    398408     */ 
     
    404414 
    405415        $this->_query = $query; 
     416    } 
     417 
     418 
     419    /** 
     420     * getCountQuery 
     421     * 
     422     * Returns the Doctrine_Query object that is used to make the count results to pager 
     423     * 
     424     * @return Doctrine_Query     Doctrine_Query object related to the pager 
     425     */ 
     426    public function getCountQuery() 
     427    { 
     428        return ($this->_countQuery !== null) ? $this->_countQuery : $this->_query; 
     429    } 
     430 
     431 
     432    /** 
     433     * setCountQuery 
     434     * 
     435     * Defines the counter query to be used by pager 
     436     * 
     437     * @param Doctrine_Query     Accepts either a Doctrine_Query object or a string  
     438     *                           (which does the Doctrine_Query class creation). 
     439     * @return void 
     440     */ 
     441    public function setCountQuery($query) 
     442    { 
     443        if (is_string($query)) { 
     444            $query = Doctrine_Query::create()->parseQuery($query); 
     445        } 
     446 
     447        $this->_countQuery = $query; 
     448 
     449        $this->_setExecuted(false); 
     450    } 
     451 
     452 
     453    /** 
     454     * getCountQueryParams 
     455     * 
     456     * Returns the params to be used by counter Doctrine_Query 
     457     * 
     458     * @return array     Doctrine_Query counter params 
     459     */ 
     460    public function getCountQueryParams($defaultParams = array()) 
     461    { 
     462        return ($this->_countQueryParams !== null) ? $this->_countQueryParams : $defaultParams; 
     463    } 
     464 
     465 
     466    /** 
     467     * setCountQueryParams 
     468     * 
     469     * Defines the params to be used by counter Doctrine_Query 
     470     * 
     471     * @param array       Optional params to be used by counter Doctrine_Query.  
     472     *                    If not defined, the params passed to execute method will be used. 
     473     * @param boolean     Optional argument that append the query param instead of overriding the existent ones. 
     474     * @return void 
     475     */ 
     476    public function setCountQueryParams($params = array(), $append = false) 
     477    { 
     478        if ($append && is_array($this->_countQueryParams)) { 
     479            $this->_countQueryParams = array_merge($this->_countQueryParams, $params); 
     480        } else { 
     481            $this->_countQueryParams = $params; 
     482        } 
     483 
     484        $this->_setExecuted(false); 
    406485    } 
    407486 
     
    419498    public function execute($params = array(), $hydrationMode = Doctrine::FETCH_RECORD) 
    420499    { 
    421         $this->_initialize($params); 
     500        if (!$this->getExecuted()) { 
     501            $this->_initialize($params); 
     502        } 
    422503 
    423504        return $this->getQuery()->execute($params, $hydrationMode); 
  • trunk/lib/Doctrine/Pager.php

    r3529 r3538  
    4040 
    4141    /** 
     42     * @var Doctrine_Query $_countQuery Doctrine_Query object related to the counter of pager 
     43     */ 
     44    protected $_countQuery; 
     45 
     46    /** 
     47     * @var array $_countQueryParams    Hold the params to be used by Doctrine_Query counter object of pager 
     48     */ 
     49    protected $_countQueryParams; 
     50 
     51    /** 
    4252     * @var integer $_numResults        Number of results found 
    4353     */ 
     
    97107    { 
    98108        // retrieve the number of items found 
    99         $count = $this->getQuery()->count($params); 
     109        $count = $this->getCountQuery()->count($this->getCountQueryParams($params)); 
    100110        $this->_setNumResults($count); 
    101111 
     
    378388     * getQuery 
    379389     * 
    380      * Returns the Doctrine_Query object related to the pager 
    381      * 
    382      * @return Doctrine_Query        Doctrine_Query object related to the pager 
     390     * Returns the Doctrine_Query collector object related to the pager 
     391     * 
     392     * @return Doctrine_Query    Doctrine_Query object related to the pager 
    383393     */ 
    384394    public function getQuery() 
     
    391401     * _setQuery 
    392402     * 
    393      * Defines the maximum number of itens per page 
    394      * 
    395      * @param $query     Accepts either a Doctrine_Query object or a string  
    396      *                   (which does the Doctrine_Query class creation). 
     403     * Defines the collector query to be used by pager 
     404     * 
     405     * @param Doctrine_Query     Accepts either a Doctrine_Query object or a string  
     406     *                           (which does the Doctrine_Query class creation). 
    397407     * @return void 
    398408     */ 
     
    404414 
    405415        $this->_query = $query; 
     416    } 
     417 
     418 
     419    /** 
     420     * getCountQuery 
     421     * 
     422     * Returns the Doctrine_Query object that is used to make the count results to pager 
     423     * 
     424     * @return Doctrine_Query     Doctrine_Query object related to the pager 
     425     */ 
     426    public function getCountQuery() 
     427    { 
     428        return ($this->_countQuery !== null) ? $this->_countQuery : $this->_query; 
     429    } 
     430 
     431 
     432    /** 
     433     * setCountQuery 
     434     * 
     435     * Defines the counter query to be used by pager 
     436     * 
     437     * @param Doctrine_Query     Accepts either a Doctrine_Query object or a string  
     438     *                           (which does the Doctrine_Query class creation). 
     439     * @return void 
     440     */ 
     441    public function setCountQuery($query) 
     442    { 
     443        if (is_string($query)) { 
     444            $query = Doctrine_Query::create()->parseQuery($query); 
     445        } 
     446 
     447        $this->_countQuery = $query; 
     448 
     449        $this->_setExecuted(false); 
     450    } 
     451 
     452 
     453    /** 
     454     * getCountQueryParams 
     455     * 
     456     * Returns the params to be used by counter Doctrine_Query 
     457     * 
     458     * @return array     Doctrine_Query counter params 
     459     */ 
     460    public function getCountQueryParams($defaultParams = array()) 
     461    { 
     462        return ($this->_countQueryParams !== null) ? $this->_countQueryParams : $defaultParams; 
     463    } 
     464 
     465 
     466    /** 
     467     * setCountQueryParams 
     468     * 
     469     * Defines the params to be used by counter Doctrine_Query 
     470     * 
     471     * @param array       Optional params to be used by counter Doctrine_Query.  
     472     *                    If not defined, the params passed to execute method will be used. 
     473     * @param boolean     Optional argument that append the query param instead of overriding the existent ones. 
     474     * @return void 
     475     */ 
     476    public function setCountQueryParams($params = array(), $append = false) 
     477    { 
     478        if ($append && is_array($this->_countQueryParams)) { 
     479            $this->_countQueryParams = array_merge($this->_countQueryParams, $params); 
     480        } else { 
     481            $this->_countQueryParams = $params; 
     482        } 
     483 
     484        $this->_setExecuted(false); 
    406485    } 
    407486 
     
    419498    public function execute($params = array(), $hydrationMode = Doctrine::FETCH_RECORD) 
    420499    { 
    421         $this->_initialize($params); 
     500        if (!$this->getExecuted()) { 
     501            $this->_initialize($params); 
     502        } 
    422503 
    423504        return $this->getQuery()->execute($params, $hydrationMode);