Development

/branches/1.4/lib/plugins/sfPropelPlugin/lib/addon/sfPropelPager.class.php

You must first sign up to be able to contribute.

root/branches/1.4/lib/plugins/sfPropelPlugin/lib/addon/sfPropelPager.class.php

Revision 27747, 4.1 kB (checked in by Kris.Wallsmith, 5 years ago)

[1.3, 1.4] fixed doctrine pager iteration (closes #7758, refs #8021)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * This class is the Propel implementation of sfPager.  It interacts with the propel record set and
13  * manages criteria.
14  *
15  * @package    sfPropelPlugin
16  * @subpackage addon
17  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
18  * @version    SVN: $Id$
19  */
20 class sfPropelPager extends sfPager
21 {
22   protected
23     $criteria               = null,
24     $peer_method_name       = 'doSelect',
25     $peer_count_method_name = 'doCount';
26
27   /**
28    * Constructor.
29    *
30    * @see sfPager
31    */
32   public function __construct($class, $maxPerPage = 10)
33   {
34     parent::__construct($class, $maxPerPage);
35
36     $this->setCriteria(new Criteria());
37     $this->tableName = constant($this->getClassPeer().'::TABLE_NAME');
38   }
39
40   /**
41    * @see sfPager
42    */
43   public function init()
44   {
45     $this->resetIterator();
46
47     $hasMaxRecordLimit = ($this->getMaxRecordLimit() !== false);
48     $maxRecordLimit = $this->getMaxRecordLimit();
49
50     $criteriaForCount = clone $this->getCriteria();
51     $criteriaForCount
52       ->setOffset(0)
53       ->setLimit(0)
54       ->clearGroupByColumns()
55     ;
56
57     $count = call_user_func(array($this->getClassPeer(), $this->getPeerCountMethod()), $criteriaForCount);
58
59     $this->setNbResults($hasMaxRecordLimit ? min($count, $maxRecordLimit) : $count);
60
61     $criteria = $this->getCriteria()
62       ->setOffset(0)
63       ->setLimit(0)
64     ;
65
66     if (0 == $this->getPage() || 0 == $this->getMaxPerPage())
67     {
68       $this->setLastPage(0);
69     }
70     else
71     {
72       $this->setLastPage(ceil($this->getNbResults() / $this->getMaxPerPage()));
73
74       $offset = ($this->getPage() - 1) * $this->getMaxPerPage();
75       $criteria->setOffset($offset);
76
77       if ($hasMaxRecordLimit)
78       {
79         $maxRecordLimit = $maxRecordLimit - $offset;
80         if ($maxRecordLimit > $this->getMaxPerPage())
81         {
82           $criteria->setLimit($this->getMaxPerPage());
83         }
84         else
85         {
86           $criteria->setLimit($maxRecordLimit);
87         }
88       }
89       else
90       {
91         $criteria->setLimit($this->getMaxPerPage());
92       }
93     }
94   }
95
96   /**
97    * @see sfPager
98    */
99   protected function retrieveObject($offset)
100   {
101     $criteriaForRetrieve = clone $this->getCriteria();
102     $criteriaForRetrieve
103       ->setOffset($offset - 1)
104       ->setLimit(1)
105     ;
106
107     $results = call_user_func(array($this->getClassPeer(), $this->getPeerMethod()), $criteriaForRetrieve);
108
109     return is_array($results) && isset($results[0]) ? $results[0] : null;
110   }
111
112   /**
113    * @see sfPager
114    */
115   public function getResults()
116   {
117     return call_user_func(array($this->getClassPeer(), $this->getPeerMethod()), $this->getCriteria());
118   }
119
120   /**
121    * Returns the peer method name.
122    *
123    * @return string
124    */
125   public function getPeerMethod()
126   {
127     return $this->peer_method_name;
128   }
129
130   /**
131    * Sets the peer method name.
132    *
133    * @param string $method A method on the current peer class
134    */
135   public function setPeerMethod($peer_method_name)
136   {
137     $this->peer_method_name = $peer_method_name;
138   }
139
140   /**
141    * Returns the peer count method name.
142    *
143    * @return string
144    */
145   public function getPeerCountMethod()
146   {
147     return $this->peer_count_method_name;
148   }
149
150   /**
151    * Sets the peer count method name.
152    *
153    * @param string $peer_count_method_name
154    */
155   public function setPeerCountMethod($peer_count_method_name)
156   {
157     $this->peer_count_method_name = $peer_count_method_name;
158   }
159
160   /**
161    * Returns the name of the current model class' peer class.
162    *
163    * @return string
164    */
165   public function getClassPeer()
166   {
167     return constant($this->class.'::PEER');
168   }
169
170   /**
171    * Returns the current Criteria.
172    *
173    * @return Criteria
174    */
175   public function getCriteria()
176   {
177     return $this->criteria;
178   }
179
180   /**
181    * Sets the Criteria for the current pager.
182    *
183    * @param Criteria $criteria
184    */
185   public function setCriteria($criteria)
186   {
187     $this->criteria = $criteria;
188   }
189 }
190
Note: See TracBrowser for help on using the browser.