Development

/branches/1.0/lib/addon/propel/sfPropelPager.class.php

You must first sign up to be able to contribute.

root/branches/1.0/lib/addon/propel/sfPropelPager.class.php

Revision 6466, 3.4 kB (checked in by fabien, 6 years ago)

fixed sfPropelPager causes an "undefined offset" notice on empty result set (closes #2500 - patch from zoltan.nemeth)

  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 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  * @package    symfony
13  * @subpackage addon
14  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
15  * @version    SVN: $Id$
16  */
17
18 /**
19  *
20  * sfPropelPager class.
21  *
22  * @package    symfony
23  * @subpackage addon
24  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
25  * @version    SVN: $Id$
26  */
27 class sfPropelPager extends sfPager
28 {
29   protected
30     $criteria               = null,
31     $peer_method_name       = 'doSelect',
32     $peer_count_method_name = 'doCount';
33
34   public function __construct($class, $maxPerPage = 10)
35   {
36     parent::__construct($class, $maxPerPage);
37
38     $this->setCriteria(new Criteria());
39     $this->tableName = constant($class.'Peer::TABLE_NAME');
40   }
41  
42   public function init()
43   {
44     $hasMaxRecordLimit = ($this->getMaxRecordLimit() !== false);
45     $maxRecordLimit = $this->getMaxRecordLimit();
46
47     $cForCount = clone $this->getCriteria();
48     $cForCount->setOffset(0);
49     $cForCount->setLimit(0);
50     $cForCount->clearGroupByColumns();
51
52     // require the model class (because autoloading can crash under some conditions)
53     if (!$classPath = sfCore::getClassPath($this->getClassPeer()))
54     {
55       throw new sfException(sprintf('Unable to find path for class "%s".', $this->getClassPeer()));
56     }
57     require_once($classPath);
58     $count = call_user_func(array($this->getClassPeer(), $this->getPeerCountMethod()), $cForCount);
59
60     $this->setNbResults($hasMaxRecordLimit ? min($count, $maxRecordLimit) : $count);
61
62     $c = $this->getCriteria();
63     $c->setOffset(0);
64     $c->setLimit(0);
65
66     if (($this->getPage() == 0 || $this->getMaxPerPage() == 0))
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       $c->setOffset($offset);
76
77       if ($hasMaxRecordLimit)
78       {
79         $maxRecordLimit = $maxRecordLimit - $offset;
80         if ($maxRecordLimit > $this->getMaxPerPage())
81         {
82           $c->setLimit($this->getMaxPerPage());
83         }
84         else
85         {
86           $c->setLimit($maxRecordLimit);
87         }
88       }
89       else
90       {
91         $c->setLimit($this->getMaxPerPage());
92       }
93     }
94   }
95
96   protected function retrieveObject($offset)
97   {
98     $cForRetrieve = clone $this->getCriteria();
99     $cForRetrieve->setOffset($offset - 1);
100     $cForRetrieve->setLimit(1);
101
102     $results = call_user_func(array($this->getClassPeer(), $this->getPeerMethod()), $cForRetrieve);
103
104     return is_array($results) && isset($results[0]) ? $results[0] : null;
105   }
106
107   public function getResults()
108   {
109     $c = $this->getCriteria();
110
111     return call_user_func(array($this->getClassPeer(), $this->getPeerMethod()), $c);
112   }
113
114   public function getPeerMethod()
115   {
116     return $this->peer_method_name;
117   }
118
119   public function setPeerMethod($peer_method_name)
120   {
121     $this->peer_method_name = $peer_method_name;
122   }
123
124   public function getPeerCountMethod()
125   {
126     return $this->peer_count_method_name;
127   }
128
129   public function setPeerCountMethod($peer_count_method_name)
130   {
131     $this->peer_count_method_name = $peer_count_method_name;
132   }
133  
134   public function getClassPeer()
135   {
136     return $this->class.'Peer';
137   }
138 }
Note: See TracBrowser for help on using the browser.