Changeset 3529

Show
Ignore:
Timestamp:
01/16/08 20:51:36 (18 months ago)
Author:
guilhermeblanco
Message:

Fixed count bug in Doctrine_Pager that was wrong counting the total of results found. Added 3 new methods: Doctrine_Pager::getExecuted (checks if the Pager was already executed), Doctrine_Pager_Layout::execute (handy access to execute Pager query without having to access Doctrine_Pager instance) and Doctrine_Pager_Layout::processPage (processes the template of a given page and returns the parsed string)

Files:
18 modified

Legend:

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

    r3395 r3529  
    2929 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL 
    3030 * @version     $Revision$ 
    31  * @link        www.phpdoctrine.com 
    32  * @since       1.0 
     31 * @link        www.phpdoctrine.org 
     32 * @since       0.9 
    3333 */ 
    3434class Doctrine_Pager 
     
    5959    protected $_lastPage; 
    6060 
     61    /** 
     62     * @var boolean $_executed          Pager was initialized (called "execute" at least once) 
     63     */ 
     64    protected $_executed; 
     65 
    6166 
    6267 
     
    6570     * 
    6671     * @param mixed $query     Accepts either a Doctrine_Query object or a string  
    67         *                        (which does the Doctrine_Query class creation). 
     72    *                        (which does the Doctrine_Query class creation). 
    6873     * @param int $page     Current page 
    6974     * @param int $maxPerPage     Maximum itens per page 
     
    7277    public function __construct($query, $page, $maxPerPage = 0) 
    7378    { 
     79        $this->_setExecuted(false); 
     80 
    7481        $this->_setQuery($query); 
    75  
    76         $this->_setMaxPerPage($maxPerPage); 
    7782        $this->_setPage($page); 
    7883 
    79         $this->_initialize(); 
     84        $this->setMaxPerPage($maxPerPage); 
    8085    } 
    8186 
     
    8691     * Initialize Pager object calculating number of results 
    8792     * 
    88      * @return void 
    89      */ 
    90     protected function _initialize() 
     93     * @param $params  Optional parameters to Doctrine_Query::execute 
     94     * @return void 
     95     */ 
     96    protected function _initialize($params = array()) 
    9197    { 
    9298        // retrieve the number of items found 
    93                 $count = $this->getQuery()->count(); 
     99        $count = $this->getQuery()->count($params); 
    94100        $this->_setNumResults($count); 
    95101 
    96102        $this->_adjustOffset(); 
     103 
     104        $this->_setExecuted(true); 
    97105    } 
    98106 
     
    107115    protected function _adjustOffset() 
    108116    { 
    109         // Define new total of pages 
    110                 $this->_setLastPage( 
    111             max(1, ceil($this->getNumResults() / $this->getMaxPerPage())) 
     117        if (!$this->getExecuted()) { 
     118            // Define new total of pages 
     119            $this->_setLastPage( 
     120                max(1, ceil($this->getNumResults() / $this->getMaxPerPage())) 
     121            ); 
     122            $offset = ($this->getPage() - 1) * $this->getMaxPerPage(); 
     123 
     124            // Assign new offset and limit to Doctrine_Query object 
     125            $p = $this->getQuery(); 
     126            $p->offset($offset); 
     127            $p->limit($this->getMaxPerPage()); 
     128        } 
     129    } 
     130 
     131 
     132    /** 
     133     * getExecuted 
     134     * 
     135     * Returns the check if Pager was already executed at least once 
     136     * 
     137     * @return boolen        Pager was executed 
     138     */ 
     139    public function getExecuted() 
     140    { 
     141        return $this->_executed; 
     142    } 
     143 
     144 
     145    /** 
     146     * _setExecuted 
     147     * 
     148     * Defines if Pager was already executed 
     149     * 
     150     * @param $executed       Pager was executed 
     151     * @return void 
     152     */ 
     153    protected function _setExecuted($executed) 
     154    { 
     155        $this->_executed = $executed; 
     156    } 
     157 
     158 
     159    /** 
     160     * getNumResults 
     161     * 
     162     * Returns the number of results found 
     163     * 
     164     * @return int        the number of results found 
     165     */ 
     166    public function getNumResults() 
     167    { 
     168        if ($this->getExecuted()) { 
     169            return $this->_numResults; 
     170        } 
     171 
     172        throw new Doctrine_Pager_Exception( 
     173            'Cannot retrieve the number of results of a not yet executed Pager query' 
    112174        ); 
    113         $offset = ($this->getPage() - 1) * $this->getMaxPerPage(); 
    114  
    115                 // Assign new offset and limit to Doctrine_Query object 
    116         $p = $this->getQuery(); 
    117         $p->offset($offset); 
    118         $p->limit($this->getMaxPerPage()); 
    119     } 
    120  
    121  
    122     /** 
    123      * getNumResults 
    124      * 
    125      * Returns the number of results found 
    126      * 
    127      * @return int        the number of results found 
    128      */ 
    129     public function getNumResults() 
    130     { 
    131         return $this->_numResults; 
    132175    } 
    133176 
     
    169212    public function getLastPage() 
    170213    { 
    171         return $this->_lastPage; 
     214        if ($this->getExecuted()) { 
     215            return $this->_lastPage; 
     216        } 
     217 
     218        throw new Doctrine_Pager_Exception( 
     219            'Cannot retrieve the last page number of a not yet executed Pager query' 
     220        ); 
    172221    } 
    173222 
     
    213262    public function getNextPage() 
    214263    { 
    215         return min($this->getPage() + 1, $this->getLastPage()); 
     264        if ($this->getExecuted()) { 
     265            return $this->_lastPage; 
     266        } 
     267 
     268        throw new Doctrine_Pager_Exception( 
     269            'Cannot retrieve the last page number of a not yet executed Pager query' 
     270        );return min($this->getPage() + 1, $this->getLastPage()); 
    216271    } 
    217272 
     
    226281    public function getPreviousPage() 
    227282    { 
    228         return max($this->getPage() - 1, $this->getFirstPage()); 
     283        if ($this->getExecuted()) { 
     284            return max($this->getPage() - 1, $this->getFirstPage()); 
     285        } 
     286 
     287        throw new Doctrine_Pager_Exception( 
     288            'Cannot retrieve the previous page number of a not yet executed Pager query' 
     289        ); 
    229290    } 
    230291 
     
    239300    public function haveToPaginate() 
    240301    { 
    241         return $this->getNumResults() > $this->getMaxPerPage(); 
     302        if ($this->getExecuted()) { 
     303            return $this->getNumResults() > $this->getMaxPerPage(); 
     304        } 
     305 
     306        throw new Doctrine_Pager_Exception( 
     307            'Cannot know if it is necessary to paginate a not yet executed Pager query' 
     308        ); 
    242309    } 
    243310 
     
    254321    { 
    255322        $this->_setPage($page); 
    256         $this->_adjustOffset(); 
     323        $this->_setExecuted(false); 
    257324    } 
    258325 
     
    295362     */ 
    296363    public function setMaxPerPage($max) 
    297     { 
    298         $this->_setMaxPerPage($max); 
    299         $this->_adjustOffset(); 
    300     } 
    301  
    302  
    303     /** 
    304      * _setMaxPerPage 
    305      * 
    306      * Defines the maximum number of itens per page 
    307      * 
    308      * @param $max       maximum number of itens per page 
    309      * @return void 
    310      */ 
    311     private function _setMaxPerPage($max) 
    312364    { 
    313365        if ($max > 0) { 
     
    318370            $this->_maxPerPage = abs($max); 
    319371        } 
     372 
     373        $this->_setExecuted(false); 
    320374    } 
    321375 
     
    340394     * 
    341395     * @param $query     Accepts either a Doctrine_Query object or a string  
    342         *                   (which does the Doctrine_Query class creation). 
     396    *                   (which does the Doctrine_Query class creation). 
    343397     * @return void 
    344398     */ 
     
    355409    /** 
    356410     * execute 
    357      * executes the query and populates the data set 
    358      * 
    359      * @param $params        Optional parameters to Doctrine_Query::execute 
    360      * @param $hydrationMode        Hyddration Mode of Doctrine_Query::execute  
    361          *                              returned ResultSet. Doctrine::Default is FETCH_RECORD 
    362      * @return Doctrine_Collection            the root collection 
     411     * 
     412     * Executes the query, populates the collection and then return it 
     413     * 
     414     * @param $params               Optional parameters to Doctrine_Query::execute 
     415     * @param $hydrationMode        Hydration Mode of Doctrine_Query::execute  
     416     *                              returned ResultSet. Doctrine::Default is FETCH_RECORD 
     417     * @return Doctrine_Collection  The root collection 
    363418     */ 
    364419    public function execute($params = array(), $hydrationMode = Doctrine::FETCH_RECORD) 
    365420    { 
     421        $this->_initialize($params); 
     422 
    366423        return $this->getQuery()->execute($params, $hydrationMode); 
    367424    } 
  • branches/0.10/lib/Doctrine/Pager/Exception.php

    r3222 r3529  
    3131 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL 
    3232 * @version     $Revision$ 
    33  * @link        www.phpdoctrine.com 
    34  * @since       1.0 
     33 * @link        www.phpdoctrine.org 
     34 * @since       0.9 
    3535 */ 
    3636class Doctrine_Pager_Exception extends Doctrine_Exception 
  • branches/0.10/lib/Doctrine/Pager/Layout.php

    r3425 r3529  
    3131 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL 
    3232 * @version     $Revision$ 
    33  * @link        www.phpdoctrine.com 
    34  * @since       1.0 
     33 * @link        www.phpdoctrine.org 
     34 * @since       0.9 
    3535 */ 
    3636class Doctrine_Pager_Layout 
     
    122122 
    123123    /** 
     124     * execute 
     125     * 
     126     * Handy method to execute the query without need to retrieve the Pager instance 
     127     * 
     128     * @param $params               Optional parameters to Doctrine_Query::execute 
     129     * @param $hydrationMode        Hydration Mode of Doctrine_Query::execute  
     130     *                              returned ResultSet. Doctrine::Default is FETCH_RECORD 
     131     * @return Doctrine_Collection  The root collection 
     132     */ 
     133    public function execute($params = array(), $hydrationMode = Doctrine::FETCH_RECORD) 
     134    { 
     135        return $this->getPager()->execute($params, $hydrationMode); 
     136    } 
     137 
     138 
     139    /** 
    124140     * getPagerRange 
    125141     * 
     
    256272        $this->_separatorTemplate = $separatorTemplate; 
    257273    } 
    258      
    259      
     274 
     275 
    260276    /** 
    261277     * addMaskReplacement 
     
    280296        } 
    281297    } 
    282      
    283      
     298 
     299 
    284300    /** 
    285301     * removeMaskReplacement 
     
    334350            // Define some optional mask values 
    335351            $options['page_number'] = $range[$i]; 
    336             $options['page'] = $range[$i]; // Handy assignment for URLs 
    337             $options['url'] = $this->_parseUrl($options); 
    338  
    339             $str .= $this->_parseTemplate($options); 
     352 
     353            $str .= $this->processPage($options); 
    340354 
    341355            // Apply separator between pages 
     
    353367    } 
    354368 
    355     /** 
    356      * simply calls display, and returns the output. 
     369 
     370    /** 
     371     * processPage 
     372     * 
     373     * Parses the template and returns the string of a processed page 
     374     * 
     375     * @param array    Optional parameters to be applied in template and url mask 
     376     * @return string  Processed template for the given page 
     377     */ 
     378    public function processPage($options = array()) 
     379    { 
     380        // Check if at least basic options are defined 
     381        if (!isset($options['page_number'])) { 
     382            throw new Doctrine_Pager_Exception( 
     383                'Cannot process template of the given page. ' . 
     384                'Missing at least one of needed parameters: \'page\' or \'page_number\'' 
     385            ); 
     386 
     387            // Should never reach here 
     388            return ''; 
     389        } 
     390 
     391        // Assign "page" options index if not defined yet 
     392        if (!isset($this->_maskReplacements['page']) && !isset($options['page'])) { 
     393            $options['page'] = $options['page_number']; 
     394        } 
     395 
     396        return $this->_parseTemplate($options); 
     397    } 
     398 
     399 
     400    /** 
     401     * Simply calls display, and returns the output. 
    357402     */ 
    358403    public function __toString() 
     
    367412     * Process the template of a given page and return the processed template 
    368413     * 
    369      * @param $options    Optional parameters to be applied in template and url mask 
     414     * @param array    Optional parameters to be applied in template and url mask 
    370415     * @return string   
    371416     */ 
     
    374419        $str = ''; 
    375420 
    376         if (isset($options['page_number']) && $options['page_number'] == $this->getPager()->getPage()) { 
     421        // If given page is the current active one 
     422        if ($options['page_number'] == $this->getPager()->getPage()) { 
    377423            $str = $this->_parseMaskReplacements($this->getSelectedTemplate()); 
    378424        } 
     
    383429        } 
    384430 
     431        // Defining "url" options index to allow {%url} mask 
     432        $options['url'] = $this->_parseUrl($options); 
     433 
    385434        $replacements = array(); 
    386          
     435 
    387436        foreach ($options as $k => $v) { 
    388437            $replacements['{%'.$k.'}'] = $v; 
  • branches/0.10/lib/Doctrine/Pager/Range.php

    r3395 r3529  
    2929 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL 
    3030 * @version     $Revision$ 
    31  * @link        www.phpdoctrine.com 
    32  * @since       1.0 
     31 * @link        www.phpdoctrine.org 
     32 * @since       0.9 
    3333 */ 
    3434abstract class Doctrine_Pager_Range 
  • branches/0.10/lib/Doctrine/Pager/Range/Jumping.php

    r3414 r3529  
    3131 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL 
    3232 * @version     $Revision$ 
    33  * @link        www.phpdoctrine.com 
    34  * @since       1.0 
     33 * @link        www.phpdoctrine.org 
     34 * @since       0.9 
    3535 */ 
    3636class Doctrine_Pager_Range_Jumping extends Doctrine_Pager_Range 
     
    9696    { 
    9797        $pager = $this->getPager(); 
    98         $page = $pager->getPage(); 
    9998 
    100         // Define initial assignments for StartPage and EndPage 
    101         $startPage = $page - ($page - 1) % $this->getChunkLength(); 
    102         $endPage = ($startPage + $this->getChunkLength()) - 1; 
     99        if ($pager->getExecuted()) { 
     100            $page = $pager->getPage(); 
    103101 
    104         // Check for EndPage out-range 
    105         if ($endPage > $pager->getLastPage()) { 
    106             $endPage = $pager->getLastPage(); 
     102            // Define initial assignments for StartPage and EndPage 
     103            $startPage = $page - ($page - 1) % $this->getChunkLength(); 
     104            $endPage = ($startPage + $this->getChunkLength()) - 1; 
     105 
     106            // Check for EndPage out-range 
     107            if ($endPage > $pager->getLastPage()) { 
     108                $endPage = $pager->getLastPage(); 
     109            } 
     110 
     111            // No need to check for out-range in start, it will never happens 
     112 
     113            return range($startPage, $endPage); 
    107114        } 
    108115 
    109         // No need to check for out-range in start, it will never happens 
    110  
    111         return range($startPage, $endPage); 
     116        throw new Doctrine_Pager_Exception( 
     117            'Cannot retrieve the range around the page of a not yet executed Pager query' 
     118        ); 
    112119    } 
    113120} 
  • branches/0.10/lib/Doctrine/Pager/Range/Sliding.php

    r3512 r3529  
    3131 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL 
    3232 * @version     $Revision$ 
    33  * @link        www.phpdoctrine.com 
    34  * @since       1.0 
     33 * @link        www.phpdoctrine.org 
     34 * @since       0.9 
    3535 */ 
    3636class Doctrine_Pager_Range_Sliding extends Doctrine_Pager_Range 
     
    101101    { 
    102102        $pager = $this->getPager(); 
    103         $page  = $pager->getPage(); 
    104         $pages = $pager->getLastPage(); 
    105103 
    106         $chunk = $this->getChunkLength(); 
    107         if ($chunk > $pages) { 
    108             $chunk = $pages; 
     104        if ($pager->getExecuted()) { 
     105            $page  = $pager->getPage(); 
     106            $pages = $pager->getLastPage(); 
     107 
     108            $chunk = $this->getChunkLength(); 
     109 
     110            if ($chunk > $pages) { 
     111                $chunk = $pages; 
     112            } 
     113 
     114            $chunkStart = $page - (floor($chunk / 2)); 
     115            $chunkEnd   = $page + (ceil($chunk / 2)-1); 
     116 
     117            if ($chunkStart < 1) { 
     118                $adjust = 1 - $chunkStart; 
     119                $chunkStart = 1; 
     120                $chunkEnd = $chunkEnd + $adjust; 
     121            } 
     122 
     123            if ($chunkEnd > $pages) { 
     124                $adjust = $chunkEnd - $pages; 
     125                $chunkStart = $chunkStart - $adjust; 
     126                $chunkEnd = $pages; 
     127            } 
     128 
     129            return range($chunkStart, $chunkEnd); 
    109130        } 
    110131 
    111         $chunkStart = $page - (floor($chunk / 2)); 
    112         $chunkEnd   = $page + (ceil($chunk / 2)-1); 
    113  
    114         if ($chunkStart < 1) { 
    115             $adjust = 1 - $chunkStart; 
    116             $chunkStart = 1; 
    117             $chunkEnd = $chunkEnd + $adjust; 
    118         } 
    119         if ($chunkEnd > $pages) { 
    120             $adjust = $chunkEnd - $pages; 
    121             $chunkStart = $chunkStart - $adjust; 
    122             $chunkEnd = $pages; 
    123         } 
    124  
    125         return range($chunkStart, $chunkEnd); 
    126  
     132        throw new Doctrine_Pager_Exception( 
     133            'Cannot retrieve the range around the page of a not yet executed Pager query' 
     134        ); 
    127135    } 
    128136} 
  • branches/0.9/lib/Doctrine/Pager.php

    r3512 r3529  
    2929 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL 
    3030 * @version     $Revision$ 
    31  * @link        www.phpdoctrine.com 
    32  * @since       1.0 
     31 * @link        www.phpdoctrine.org 
     32 * @since       0.9 
    3333 */ 
    3434class Doctrine_Pager 
     
    5959    protected $_lastPage; 
    6060 
     61    /** 
     62     * @var boolean $_executed          Pager was initialized (called "execute" at least once) 
     63     */ 
     64    protected $_executed; 
     65 
    6166 
    6267 
     
    6570     * 
    6671     * @param mixed $query     Accepts either a Doctrine_Query object or a string  
    67         *                        (which does the Doctrine_Query class creation). 
     72    *                        (which does the Doctrine_Query class creation). 
    6873     * @param int $page     Current page 
    6974     * @param int $maxPerPage     Maximum itens per page 
     
    7277    public function __construct($query, $page, $maxPerPage = 0) 
    7378    { 
     79        $this->_setExecuted(false); 
     80 
    7481        $this->_setQuery($query); 
    75  
    76         $this->_setMaxPerPage($maxPerPage); 
    7782        $this->_setPage($page); 
    7883 
    79         $this->_initialize(); 
     84        $this->setMaxPerPage($maxPerPage); 
    8085    } 
    8186 
     
    8691     * Initialize Pager object calculating number of results 
    8792     * 
    88      * @return void 
    89      */ 
    90     protected function _initialize() 
     93     * @param $params  Optional parameters to Doctrine_Query::execute 
     94     * @return void 
     95     */ 
     96    protected function _initialize($params = array()) 
    9197    { 
    9298        // retrieve the number of items found 
    93                 $count = $this->getQuery()->count(); 
     99        $count = $this->getQuery()->count($params); 
    94100        $this->_setNumResults($count); 
    95101 
    96102        $this->_adjustOffset(); 
     103 
     104        $this->_setExecuted(true); 
    97105    } 
    98106 
     
    107115    protected function _adjustOffset() 
    108116    { 
    109         // Define new total of pages 
    110                 $this->_setLastPage( 
    111             max(1, ceil($this->getNumResults() / $this->getMaxPerPage())) 
     117        if (!$this->getExecuted()) { 
     118            // Define new total of pages 
     119            $this->_setLastPage( 
     120                max(1, ceil($this->getNumResults() / $this->getMaxPerPage())) 
     121            ); 
     122            $offset = ($this->getPage() - 1) * $this->getMaxPerPage(); 
     123 
     124            // Assign new offset and limit to Doctrine_Query object 
     125            $p = $this->getQuery(); 
     126            $p->offset($offset); 
     127            $p->limit($this->getMaxPerPage()); 
     128        } 
     129    } 
     130 
     131 
     132    /** 
     133     * getExecuted 
     134     * 
     135     * Returns the check if Pager was already executed at least once 
     136     * 
     137     * @return boolen        Pager was executed 
     138     */ 
     139    public function getExecuted() 
     140    { 
     141        return $this->_executed; 
     142    } 
     143 
     144 
     145    /** 
     146     * _setExecuted 
     147     * 
     148     * Defines if Pager was already executed 
     149     * 
     150     * @param $executed       Pager was executed 
     151     * @return void 
     152     */ 
     153    protected function _setExecuted($executed) 
     154    { 
     155        $this->_executed = $executed; 
     156    } 
     157 
     158 
     159    /** 
     160     * getNumResults 
     161     * 
     162     * Returns the number of results found 
     163     * 
     164     * @return int        the number of results found 
     165     */ 
     166    public function getNumResults() 
     167    { 
     168        if ($this->getExecuted()) { 
     169            return $this->_numResults; 
     170        } 
     171 
     172        throw new Doctrine_Pager_Exception( 
     173            'Cannot retrieve the number of results of a not yet executed Pager query' 
    112174        ); 
    113         $offset = ($this->getPage() - 1) * $this->getMaxPerPage(); 
    114  
    115                 // Assign new offset and limit to Doctrine_Query object 
    116         $p = $this->getQuery(); 
    117         $p->offset($offset); 
    118         $p->limit($this->getMaxPerPage()); 
    119     } 
    120  
    121  
    122     /** 
    123      * getNumResults 
    124      * 
    125      * Returns the number of results found 
    126      * 
    127      * @return int        the number of results found 
    128      */ 
    129     public function getNumResults() 
    130     { 
    131         return $this->_numResults; 
    132175    } 
    133176 
     
    169212    public function getLastPage() 
    170213    { 
    171         return $this->_lastPage; 
     214        if ($this->getExecuted()) { 
     215            return $this->_lastPage; 
     216        } 
     217 
     218        throw new Doctrine_Pager_Exception( 
     219            'Cannot retrieve the last page number of a not yet executed Pager query' 
     220        ); 
    172221    } 
    173222 
     
    213262    public function getNextPage() 
    214263    { 
    215         return min($this->getPage() + 1, $this->getLastPage()); 
     264        if ($this->getExecuted()) { 
     265            return $this->_lastPage; 
     266        } 
     267 
     268        throw new Doctrine_Pager_Exception( 
     269            'Cannot retrieve the last page number of a not yet executed Pager query' 
     270        );return min($this->getPage() + 1, $this->getLastPage()); 
    216271    } 
    217272 
     
    226281    public function getPreviousPage() 
    227282    { 
    228         return max($this->getPage() - 1, $this->getFirstPage()); 
     283        if ($this->getExecuted()) { 
     284            return max($this->getPage() - 1, $this->getFirstPage()); 
     285        } 
     286 
     287        throw new Doctrine_Pager_Exception( 
     288            'Cannot retrieve the previous page number of a not yet executed Pager query' 
     289        ); 
    229290    } 
    230291 
     
    239300    public function haveToPaginate() 
    240301    { 
    241         return $this->getNumResults() > $this->getMaxPerPage(); 
     302        if ($this->getExecuted()) { 
     303            return $this->getNumResults() > $this->getMaxPerPage(); 
     304        } 
     305 
     306        throw new Doctrine_Pager_Exception( 
     307            'Cannot know if it is necessary to paginate a not yet executed Pager query' 
     308        ); 
    242309    } 
    243310 
     
    254321    { 
    255322        $this->_setPage($page); 
    256         $this->_adjustOffset(); 
     323        $this->_setExecuted(false); 
    257324    } 
    258325 
     
    295362     */ 
    296363    public function setMaxPerPage($max) 
    297     { 
    298         $this->_setMaxPerPage($max); 
    299         $this->_adjustOffset(); 
    300     } 
    301  
    302  
    303     /** 
    304      * _setMaxPerPage 
    305      * 
    306      * Defines the maximum number of itens per page 
    307      * 
    308      * @param $max       maximum number of itens per page 
    309      * @return void 
    310      */ 
    311     private function _setMaxPerPage($max) 
    312364    { 
    313365        if ($max > 0) { 
     
    318370            $this->_maxPerPage = abs($max); 
    319371        } 
     372 
     373        $this->_setExecuted(false); 
    320374    } 
    321375 
     
    340394     * 
    341395     * @param $query     Accepts either a Doctrine_Query object or a string  
    342         *                   (which does the Doctrine_Query class creation). 
     396    *                   (which does the Doctrine_Query class creation). 
    343397     * @return void 
    344398     */ 
     
    355409    /** 
    356410     * execute 
    357      * executes the query and populates the data set 
    358      * 
    359      * @param $params        Optional parameters to Doctrine_Query::execute 
    360      * @param $hydrationMode        Hyddration Mode of Doctrine_Query::execute  
    361          *                              returned ResultSet. Doctrine::Default is FETCH_RECORD 
    362      * @return Doctrine_Collection            the root collection 
     411     * 
     412     * Executes the query, populates the collection and then return it 
     413     * 
     414     * @param $params               Optional parameters to Doctrine_Query::execute 
     415     * @param $hydrationMode        Hydration Mode of Doctrine_Query::execute  
     416     *                              returned ResultSet. Doctrine::Default is FETCH_RECORD 
     417     * @return Doctrine_Collection  The root collection 
    363418     */ 
    364419    public function execute($params = array(), $hydrationMode = Doctrine::FETCH_RECORD) 
    365420    { 
     421        $this->_initialize($params); 
     422 
    366423        return $this->getQuery()->execute($params, $hydrationMode); 
    367424    } 
  • branches/0.9/lib/Doctrine/Pager/Exception.php

    r3512 r3529  
    3131 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL 
    3232 * @version     $Revision$ 
    33  * @link        www.phpdoctrine.com 
    34  * @since       1.0 
     33 * @link        www.phpdoctrine.org 
     34 * @since       0.9 
    3535 */ 
    3636class Doctrine_Pager_Exception extends Doctrine_Exception 
  • branches/0.9/lib/Doctrine/Pager/Layout.php

    r3512 r3529  
    3131 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL 
    3232 * @version     $Revision$ 
    33  * @link        www.phpdoctrine.com 
    34  * @since       1.0 
     33 * @link        www.phpdoctrine.org 
     34 * @since       0.9 
    3535 */ 
    3636class Doctrine_Pager_Layout 
     
    122122 
    123123    /** 
     124     * execute 
     125     * 
     126     * Handy method to execute the query without need to retrieve the Pager instance 
     127     * 
     128     * @param $params               Optional parameters to Doctrine_Query::execute 
     129     * @param $hydrationMode        Hydration Mode of Doctrine_Query::execute  
     130     *                              returned ResultSet. Doctrine::Default is FETCH_RECORD 
     131     * @return Doctrine_Collection  The root collection 
     132     */ 
     133    public function execute($params = array(), $hydrationMode = Doctrine::FETCH_RECORD) 
     134    { 
     135        return $this->getPager()->execute($params, $hydrationMode); 
     136    } 
     137 
     138 
     139    /** 
    124140     * getPagerRange 
    125141     * 
     
    256272        $this->_separatorTemplate = $separatorTemplate; 
    257273    } 
    258      
    259      
     274 
     275 
    260276    /** 
    261277     * addMaskReplacement 
     
    280296        } 
    281297    } 
    282      
    283      
     298 
     299 
    284300    /** 
    285301     * removeMaskReplacement 
     
    334350            // Define some optional mask values 
    335351            $options['page_number'] = $range[$i]; 
    336             $options['page'] = $range[$i]; // Handy assignment for URLs 
    337             $options['url'] = $this->_parseUrl($options); 
    338  
    339             $str .= $this->_parseTemplate($options); 
     352 
     353            $str .= $this->processPage($options); 
    340354 
    341355            // Apply separator between pages 
     
    353367    } 
    354368 
    355     /** 
    356      * simply calls display, and returns the output. 
     369 
     370    /** 
     371     * processPage 
     372     * 
     373     * Parses the template and returns the string of a processed page 
     374     * 
     375     * @param array    Optional parameters to be applied in template and url mask 
     376     * @return string  Processed template for the given page 
     377     */ 
     378    public function processPage($options = array()) 
     379    { 
     380        // Check if at least basic options are defined 
     381        if (!isset($options['page_number'])) { 
     382            throw new Doctrine_Pager_Exception( 
     383                'Cannot process template of the given page. ' . 
     384                'Missing at least one of needed parameters: \'page\' or \'page_number\'' 
     385            ); 
     386 
     387            // Should never reach here 
     388            return ''; 
     389        } 
     390 
     391        // Assign "page" options index if not defined yet 
     392        if (!isset($this->_maskReplacements['page']) && !isset($options['page'])) { 
     393            $options['page'] = $options['page_number']; 
     394        } 
     395 
     396        return $this->_parseTemplate($options); 
     397    } 
     398 
     399 
     400    /** 
     401     * Simply calls display, and returns the output. 
    357402     */ 
    358403    public function __toString() 
     
    367412     * Process the template of a given page and return the processed template 
    368413     * 
    369      * @param $options    Optional parameters to be applied in template and url mask 
     414     * @param array    Optional parameters to be applied in template and url mask 
    370415     * @return string   
    371416     */ 
     
    374419        $str = ''; 
    375420 
    376         if (isset($options['page_number']) && $options['page_number'] == $this->getPager()->getPage()) { 
     421        // If given page is the current active one 
     422        if ($options['page_number'] == $this->getPager()->getPage()) { 
    377423            $str = $this->_parseMaskReplacements($this->getSelectedTemplate()); 
    378424        } 
     
    383429        } 
    384430 
     431        // Defining "url" options index to allow {%url} mask 
     432        $options['url'] = $this->_parseUrl($options); 
     433 
    385434        $replacements = array(); 
    386          
     435 
    387436        foreach ($options as $k => $v) { 
    388437            $replacements['{%'.$k.'}'] = $v; 
  • branches/0.9/lib/Doctrine/Pager/Range.php

    r3512 r3529  
    2929 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL 
    3030 * @version     $Revision$ 
    31  * @link        www.phpdoctrine.com 
    32  * @since       1.0 
     31 * @link        www.phpdoctrine.org 
     32 * @since       0.9 
    3333 */ 
    3434abstract class Doctrine_Pager_Range 
  • branches/0.9/lib/Doctrine/Pager/Range/Jumping.php

    r3512 r3529  
    3131 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL 
    3232 * @version     $Revision$ 
    33  * @link        www.phpdoctrine.com 
    34  * @since       1.0 
     33 * @link        www.phpdoctrine.org 
     34 * @since       0.9 
    3535 */ 
    3636class Doctrine_Pager_Range_Jumping extends Doctrine_Pager_Range 
     
    9696    { 
    9797        $pager = $this->getPager(); 
    98         $page = $pager->getPage(); 
    9998 
    100         // Define initial assignments for StartPage and EndPage 
    101         $startPage = $page - ($page - 1) % $this->getChunkLength(); 
    102         $endPage = ($startPage + $this->getChunkLength()) - 1; 
     99        if ($pager->getExecuted()) { 
     100            $page = $pager->getPage(); 
    103101 
    104         // Check for EndPage out-range 
    105         if ($endPage > $pager->getLastPage()) { 
    106             $endPage = $pager->getLastPage(); 
     102            // Define initial assignments for StartPage and EndPage 
     103            $startPage = $page - ($page - 1) % $this->getChunkLength(); 
     104            $endPage = ($startPage + $this->getChunkLength()) - 1; 
     105 
     106            // Check for EndPage out-range 
     107            if ($endPage > $pager->getLastPage()) { 
     108                $endPage = $pager->getLastPage(); 
     109            } 
     110 
     111            // No need to check for out-range in start, it will never happens 
     112 
     113            return range($startPage, $endPage); 
    107114        } 
    108115 
    109         // No need to check for out-range in start, it will never happens 
    110  
    111         return range($startPage, $endPage); 
     116        throw new Doctrine_Pager_Exception( 
     117            'Cannot retrieve the range around the page of a not yet executed Pager query' 
     118        ); 
    112119    } 
    113120} 
  • branches/0.9/lib/Doctrine/Pager/Range/Sliding.php

    r3512 r3529  
    3131 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL 
    3232 * @version     $Revision$ 
    33  * @link        www.phpdoctrine.com 
    34  * @since       1.0 
     33 * @link        www.phpdoctrine.org 
     34 * @since       0.9 
    3535 */ 
    3636class Doctrine_Pager_Range_Sliding extends Doctrine_Pager_Range 
     
    101101    { 
    102102        $pager = $this->getPager(); 
    103         $page  = $pager->getPage(); 
    104         $pages = $pager->getLastPage(); 
    105103 
    106         $chunk = $this->getChunkLength(); 
    107         if ($chunk > $pages) { 
    108             $chunk = $pages; 
     104        if ($pager->getExecuted()) { 
     105            $page  = $pager->getPage(); 
     106            $pages = $pager->getLastPage(); 
     107 
     108            $chunk = $this->getChunkLength(); 
     109 
     110            if ($chunk > $pages) { 
     111                $chunk = $pages; 
     112            } 
     113 
     114            $chunkStart = $page - (floor($chunk / 2)); 
     115            $chunkEnd   = $page + (ceil($chunk / 2)-1); 
     116 
     117            if ($chunkStart < 1) { 
     118                $adjust = 1 - $chunkStart; 
     119                $chunkStart = 1; 
     120                $chunkEnd = $chunkEnd + $adjust; 
     121            } 
     122 
     123            if ($chunkEnd > $pages) { 
     124                $adjust = $chunkEnd - $pages; 
     125                $chunkStart = $chunkStart - $adjust; 
     126                $chunkEnd = $pages; 
     127            } 
     128 
     129            return range($chunkStart, $chunkEnd); 
    109130        } 
    110131 
    111         $chunkStart = $page - (floor($chunk / 2)); 
    112         $chunkEnd   = $page + (ceil($chunk / 2)-1); 
    113  
    114         if ($chunkStart < 1) { 
    115             $adjust = 1 - $chunkStart; 
    116             $chunkStart = 1; 
    117             $chunkEnd = $chunkEnd + $adjust; 
    118         } 
    119         if ($chunkEnd > $pages) { 
    120             $adjust = $chunkEnd - $pages; 
    121             $chunkStart = $chunkStart - $adjust; 
    122             $chunkEnd = $pages; 
    123         } 
    124  
    125         return range($chunkStart, $chunkEnd); 
    126  
     132        throw new Doctrine_Pager_Exception( 
     133            'Cannot retrieve the range around the page of a not yet executed Pager query' 
     134        ); 
    127135    } 
    128136} 
  • trunk/lib/Doctrine/Pager.php

    r3458 r3529  
    2929 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL 
    3030 * @version     $Revision$ 
    31  * @link        www.phpdoctrine.com 
    32  * @since       1.0 
     31 * @link        www.phpdoctrine.org 
     32 * @since       0.9 
    3333 */ 
    3434class Doctrine_Pager 
     
    5959    protected $_lastPage; 
    6060 
     61    /** 
     62     * @var boolean $_executed          Pager was initialized (called "execute" at least once) 
     63     */ 
     64    protected $_executed; 
     65 
    6166 
    6267 
     
    6570     * 
    6671     * @param mixed $query     Accepts either a Doctrine_Query object or a string  
    67         *                        (which does the Doctrine_Query class creation). 
     72    *                        (which does the Doctrine_Query class creation). 
    6873     * @param int $page     Current page 
    6974     * @param int $maxPerPage     Maximum itens per page 
     
    7277    public function __construct($query, $page, $maxPerPage = 0) 
    7378    { 
     79        $this->_setExecuted(false); 
     80 
    7481        $this->_setQuery($query); 
    75  
    76         $this->_setMaxPerPage($maxPerPage); 
    7782        $this->_setPage($page); 
    7883 
    79         $this->_initialize(); 
     84        $this->setMaxPerPage($maxPerPage); 
    8085    } 
    8186 
     
    8691     * Initialize Pager object calculating number of results 
    8792     * 
    88      * @return void 
    89      */ 
    90     protected function _initialize() 
     93     * @param $params  Optional parameters to Doctrine_Query::execute 
     94     * @return void 
     95     */ 
     96    protected function _initialize($params = array()) 
    9197    { 
    9298        // retrieve the number of items found 
    93                 $count = $this->getQuery()->count(); 
     99        $count = $this->getQuery()->count($params); 
    94100        $this->_setNumResults($count); 
    95101 
    96102        $this->_adjustOffset(); 
     103 
     104        $this->_setExecuted(true); 
    97105    } 
    98106 
     
    107115    protected function _adjustOffset() 
    108116    { 
    109         // Define new total of pages 
    110                 $this->_setLastPage( 
    111             max(1, ceil($this->getNumResults() / $this->getMaxPerPage())) 
     117        if (!$this->getExecuted()) { 
     118            // Define new total of pages 
     119            $this->_setLastPage( 
     120                max(1, ceil($this->getNumResults() / $this->getMaxPerPage())) 
     121            ); 
     122            $offset = ($this->getPage() - 1) * $this->getMaxPerPage(); 
     123 
     124            // Assign new offset and limit to Doctrine_Query object 
     125            $p = $this->getQuery(); 
     126            $p->offset($offset); 
     127            $p->limit($this->getMaxPerPage()); 
     128        } 
     129    } 
     130 
     131 
     132    /** 
     133     * getExecuted 
     134     * 
     135     * Returns the check if Pager was already executed at least once 
     136     * 
     137     * @return boolen        Pager was executed 
     138     */ 
     139    public function getExecuted() 
     140    { 
     141        return $this->_executed; 
     142    } 
     143 
     144 
     145    /** 
     146     * _setExecuted 
     147     * 
     148     * Defines if Pager was already executed 
     149     * 
     150     * @param $executed       Pager was executed 
     151     * @return void 
     152     */ 
     153    protected function _setExecuted($executed) 
     154    { 
     155        $this->_executed = $executed; 
     156    } 
     157 
     158 
     159    /** 
     160     * getNumResults 
     161     * 
     162     * Returns the number of results found 
     163     * 
     164     * @return int        the number of results found 
     165     */ 
     166    public function getNumResults() 
     167    { 
     168        if ($this->getExecuted()) { 
     169            return $this->_numResults; 
     170        } 
     171 
     172        throw new Doctrine_Pager_Exception( 
     173            'Cannot retrieve the number of results of a not yet executed Pager query' 
    112174        ); 
    113         $offset = ($this->getPage() - 1) * $this->getMaxPerPage(); 
    114  
    115                 // Assign new offset and limit to Doctrine_Query object 
    116         $p = $this->getQuery(); 
    117         $p->offset($offset); 
    118         $p->limit($this->getMaxPerPage()); 
    119     } 
    120  
    121  
    122     /** 
    123      * getNumResults 
    124      * 
    125      * Returns the number of results found 
    126      * 
    127      * @return int        the number of results found 
    128      */ 
    129     public function getNumResults() 
    130     { 
    131         return $this->_numResults; 
    132175    } 
    133176 
     
    169212    public function getLastPage() 
    170213    { 
    171         return $this->_lastPage; 
     214        if ($this->getExecuted()) { 
     215            return $this->_lastPage; 
     216        } 
     217 
     218        throw new Doctrine_Pager_Exception( 
     219            'Cannot retrieve the last page number of a not yet executed Pager query' 
     220        ); 
    172221    } 
    173222 
     
    213262    public function getNextPage() 
    214263    { 
    215         return min($this->getPage() + 1, $this->getLastPage()); 
     264        if ($this->getExecuted()) { 
     265            return $this->_lastPage; 
     266        } 
     267 
     268        throw new Doctrine_Pager_Exception( 
     269            'Cannot retrieve the last page number of a not yet executed Pager query' 
     270        );return min($this->getPage() + 1, $this->getLastPage()); 
    216271    } 
    217272 
     
    226281    public function getPreviousPage() 
    227282    { 
    228         return max($this->getPage() - 1, $this->getFirstPage()); 
     283        if ($this->getExecuted()) { 
     284            return max($this->getPage() - 1, $this->getFirstPage()); 
     285        } 
     286 
     287        throw new Doctrine_Pager_Exception( 
     288            'Cannot retrieve the previous page number of a not yet executed Pager query' 
     289        ); 
    229290    } 
    230291 
     
    239300    public function haveToPaginate() 
    240301    { 
    241         return $this->getNumResults() > $this->getMaxPerPage(); 
     302        if ($this->getExecuted()) { 
     303            return $this->getNumResults() > $this->getMaxPerPage(); 
     304        } 
     305 
     306        throw new Doctrine_Pager_Exception( 
     307            'Cannot know if it is necessary to paginate a not yet executed Pager query' 
     308        ); 
    242309    } 
    243310 
     
    254321    { 
    255322        $this->_setPage($page); 
    256         $this->_adjustOffset(); 
     323        $this->_setExecuted(false); 
    257324    } 
    258325 
     
    295362     */ 
    296363    public function setMaxPerPage($max) 
    297     { 
    298         $this->_setMaxPerPage($max); 
    299         $this->_adjustOffset(); 
    300     } 
    301  
    302  
    303     /** 
    304      * _setMaxPerPage 
    305      * 
    306      * Defines the maximum number of itens per page 
    307      * 
    308      * @param $max       maximum number of itens per page 
    309      * @return void 
    310      */ 
    311     private function _setMaxPerPage($max) 
    312364    { 
    313365        if ($max > 0) { 
     
    318370            $this->_maxPerPage = abs($max); 
    319371        } 
     372 
     373        $this->_setExecuted(false); 
    320374    } 
    321375 
     
    340394     * 
    341395     * @param $query     Accepts either a Doctrine_Query object or a string  
    342         *                   (which does the Doctrine_Query class creation). 
     396    *                   (which does the Doctrine_Query class creation). 
    343397     * @return void 
    344398     */ 
     
    355409    /** 
    356410     * execute 
    357      * executes the query and populates the data set 
    358      * 
    359      * @param $params        Optional parameters to Doctrine_Query::execute 
    360      * @param $hydrationMode        Hyddration Mode of Doctrine_Query::execute  
    361          *                              returned ResultSet. Doctrine::Default is FETCH_RECORD 
    362      * @return Doctrine_Collection            the root collection 
     411     * 
     412     * Executes the query, populates the collection and then return it 
     413     * 
     414     * @param $params               Optional parameters to Doctrine_Query::execute 
     415     * @param $hydrationMode        Hydration Mode of Doctrine_Query::execute  
     416     *                              returned ResultSet. Doctrine::Default is FETCH_RECORD 
     417     * @return Doctrine_Collection  The root collection 
    363418     */ 
    364419    public function execute($params = array(), $hydrationMode = Doctrine::FETCH_RECORD) 
    365420    { 
     421        $this->_initialize($params); 
     422 
    366423        return $this->getQuery()->execute($params, $hydrationMode); 
    367424    } 
  • trunk/lib/Doctrine/Pager/Exception.php

    r3458 r3529  
    3131 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL 
    3232 * @version     $Revision$ 
    33  * @link        www.phpdoctrine.com 
    34  * @since       1.0 
     33 * @link        www.phpdoctrine.org 
     34 * @since       0.9 
    3535 */ 
    3636class Doctrine_Pager_Exception extends Doctrine_Exception 
  • trunk/lib/Doctrine/Pager/Layout.php

    r3458 r3529  
    3131 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL 
    3232 * @version     $Revision$ 
    33  * @link        www.phpdoctrine.com 
    34  * @since       1.0 
     33 * @link        www.phpdoctrine.org 
     34 * @since       0.9 
    3535 */ 
    3636class Doctrine_Pager_Layout 
     
    122122 
    123123    /** 
     124     * execute 
     125     * 
     126     * Handy method to execute the query without need to retrieve the Pager instance 
     127     * 
     128     * @param $params               Optional parameters to Doctrine_Query::execute 
     129     * @param $hydrationMode        Hydration Mode of Doctrine_Query::execute  
     130     *                              returned ResultSet. Doctrine::Default is FETCH_RECORD 
     131     * @return Doctrine_Collection  The root collection 
     132     */ 
     133    public function execute($params = array(), $hydrationMode = Doctrine::FETCH_RECORD) 
     134    { 
     135        return $this->getPager()->execute($params, $hydrationMode); 
     136    } 
     137 
     138 
     139    /** 
    124140     * getPagerRange 
    125141     * 
     
    256272        $this->_separatorTemplate = $separatorTemplate; 
    257273    } 
    258      
    259      
     274 
     275 
    260276    /** 
    261277     * addMaskReplacement 
     
    280296        } 
    281297    } 
    282      
    283      
     298 
     299 
    284300    /** 
    285301     * removeMaskReplacement 
     
    334350            // Define some optional mask values 
    335351            $options['page_number'] = $range[$i]; 
    336             $options['page'] = $range[$i]; // Handy assignment for URLs 
    337             $options['url'] = $this->_parseUrl($options); 
    338  
    339             $str .= $this->_parseTemplate($options); 
     352 
     353            $str .= $this->processPage($options); 
    340354 
    341355            // Apply separator between pages 
     
    353367    } 
    354368 
    355     /** 
    356      * simply calls display, and returns the output. 
     369 
     370    /** 
     371     * processPage 
     372     * 
     373     * Parses the template and returns the string of a processed page 
     374     * 
     375     * @param array    Optional parameters to be applied in template and url mask 
     376     * @return string  Processed template for the given page 
     377     */ 
     378    public function processPage($options = array()) 
     379    { 
     380        // Check if at least basic options are defined 
     381        if (!isset($options['page_number'])) { 
     382            throw new Doctrine_Pager_Exception( 
     383                'Cannot process template of the given page. ' . 
     384                'Missing at least one of needed parameters: \'page\' or \'page_number\'' 
     385            ); 
     386 
     387            // Should never reach here 
     388            return ''; 
     389        } 
     390 
     391        // Assign "page" options index if not defined yet 
     392        if (!isset($this->_maskReplacements['page']) && !isset($options['page'])) { 
     393            $options['page'] = $options['page_number']; 
     394        } 
     395 
     396        return $this->_parseTemplate($options); 
     397    } 
     398 
     399 
     400    /** 
     401     * Simply calls display, and returns the output. 
    357402     */ 
    358403    public function __toString() 
     
    367412     * Process the template of a given page and return the processed template 
    368413     * 
    369      * @param $options    Optional parameters to be applied in template and url mask 
     414     * @param array    Optional parameters to be applied in template and url mask 
    370415     * @return string   
    371416     */ 
     
    374419        $str = ''; 
    375420 
    376         if (isset($options['page_number']) && $options['page_number'] == $this->getPager()->getPage()) { 
     421        // If given page is the current active one 
     422        if ($options['page_number'] == $this->getPager()->getPage()) { 
    377423            $str = $this->_parseMaskReplacements($this->getSelectedTemplate()); 
    378424        } 
     
    383429        } 
    384430 
     431        // Defining "url" options index to allow {%url} mask 
     432        $options['url'] = $this->_parseUrl($options); 
     433 
    385434        $replacements = array(); 
    386          
     435 
    387436        foreach ($options as $k => $v) { 
    388437            $replacements['{%'.$k.'}'] = $v; 
  • trunk/lib/Doctrine/Pager/Range.php

    r3458 r3529  
    2929 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL 
    3030 * @version     $Revision$ 
    31  * @link        www.phpdoctrine.com 
    32  * @since       1.0 
     31 * @link        www.phpdoctrine.org 
     32 * @since       0.9 
    3333 */ 
    3434abstract class Doctrine_Pager_Range 
  • trunk/lib/Doctrine/Pager/Range/Jumping.php

    r3458 r3529  
    3131 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL 
    3232 * @version     $Revision$ 
    33  * @link        www.phpdoctrine.com 
    34  * @since       1.0 
     33 * @link        www.phpdoctrine.org 
     34 * @since       0.9 
    3535 */ 
    3636class Doctrine_Pager_Range_Jumping extends Doctrine_Pager_Range 
     
    9696    { 
    9797        $pager = $this->getPager(); 
    98         $page = $pager->getPage(); 
    9998 
    100         // Define initial assignments for StartPage and EndPage 
    101         $startPage = $page - ($page - 1) % $this->getChunkLength(); 
    102         $endPage = ($startPage + $this->getChunkLength()) - 1; 
     99        if ($pager->getExecuted()) { 
     100            $page = $pager->getPage(); 
    103101 
    104         // Check for EndPage out-range 
    105         if ($endPage > $pager->getLastPage()) { 
    106             $endPage = $pager->getLastPage(); 
     102            // Define initial assignments for StartPage and EndPage 
     103            $startPage = $page - ($page - 1) % $this->getChunkLength(); 
     104            $endPage = ($startPage + $this->getChunkLength()) - 1; 
     105 
     106            // Check for EndPage out-range 
     107            if ($endPage > $pager->getLastPage()) { 
     108                $endPage = $pager->getLastPage(); 
     109            } 
     110 
     111            // No need to check for out-range in start, it will never happens 
     112 
     113            return range($startPage, $endPage); 
    107114        } 
    108115 
    109         // No need to check for out-range in start, it will never happens 
    110  
    111         return range($startPage, $endPage); 
     116        throw new Doctrine_Pager_Exception( 
     117            'Cannot retrieve the range around the page of a not yet executed Pager query' 
     118        ); 
    112119    } 
    113120} 
  • trunk/lib/Doctrine/Pager/Range/Sliding.php

    r3510 r3529  
    3131 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL 
    3232 * @version     $Revision$ 
    33  * @link        www.phpdoctrine.com 
    34  * @since       1.0 
     33 * @link        www.phpdoctrine.org 
     34 * @since       0.9 
    3535 */ 
    3636class Doctrine_Pager_Range_Sliding extends Doctrine_Pager_Range 
     
    101101    { 
    102102        $pager = $this->getPager(); 
    103         $page  = $pager->getPage(); 
    104         $pages = $pager->getLastPage(); 
    105103 
    106         $chunk = $this->getChunkLength(); 
    107         if ($chunk > $pages) { 
    108             $chunk = $pages; 
     104        if ($pager->getExecuted()) { 
     105            $page  = $pager->getPage(); 
     106            $pages = $pager->getLastPage(); 
     107 
     108            $chunk = $this->getChunkLength(); 
     109 
     110            if ($chunk > $pages) { 
     111                $chunk = $pages; 
     112            } 
     113 
     114            $chunkStart = $page - (floor($chunk / 2)); 
     115            $chunkEnd   = $page + (ceil($chunk / 2)-1); 
     116 
     117            if ($chunkStart < 1) { 
     118                $adjust = 1 - $chunkStart; 
     119                $chunkStart = 1; 
     120                $chunkEnd = $chunkEnd + $adjust; 
     121            } 
     122 
     123            if ($chunkEnd > $pages) { 
     124                $adjust = $chunkEnd - $pages; 
     125                $chunkStart = $chunkStart - $adjust; 
     126                $chunkEnd = $pages; 
     127            } 
     128 
     129            return range($chunkStart, $chunkEnd); 
    109130        } 
    110131 
    111         $chunkStart = $page - (floor($chunk / 2)); 
    112         $chunkEnd   = $page + (ceil($chunk / 2)-1); 
    113  
    114         if ($chunkStart < 1) { 
    115             $adjust = 1 - $chunkStart; 
    116             $chunkStart = 1; 
    117             $chunkEnd = $chunkEnd + $adjust; 
    118         } 
    119         if ($chunkEnd > $pages) { 
    120             $adjust = $chunkEnd - $pages; 
    121             $chunkStart = $chunkStart - $adjust; 
    122             $chunkEnd = $pages; 
    123         } 
    124  
    125         return range($chunkStart, $chunkEnd); 
    126  
     132        throw new Doctrine_Pager_Exception( 
     133            'Cannot retrieve the range around the page of a not yet executed Pager query' 
     134        ); 
    127135    } 
    128136}