Development

/branches/1.1/lib/plugins/sfPropelPlugin/lib/propel/sfPropelPager.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/plugins/sfPropelPlugin/lib/propel/sfPropelPager.class.php

Revision 7639, 3.1 kB (checked in by fabien, 6 years ago)

removed hack needed for old PHP 5 version

  • 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  *
13  * sfPropelPager class.
14  *
15  * @package    symfony
16  * @subpackage propel
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   public function __construct($class, $maxPerPage = 10)
28   {
29     parent::__construct($class, $maxPerPage);
30
31     $this->setCriteria(new Criteria());
32     $this->tableName = constant($class.'Peer::TABLE_NAME');
33   }
34  
35   public function init()
36   {
37     $hasMaxRecordLimit = ($this->getMaxRecordLimit() !== false);
38     $maxRecordLimit = $this->getMaxRecordLimit();
39
40     $cForCount = clone $this->getCriteria();
41     $cForCount->setOffset(0);
42     $cForCount->setLimit(0);
43     $cForCount->clearGroupByColumns();
44
45     $count = call_user_func(array($this->getClassPeer(), $this->getPeerCountMethod()), $cForCount);
46
47     $this->setNbResults($hasMaxRecordLimit ? min($count, $maxRecordLimit) : $count);
48
49     $c = $this->getCriteria();
50     $c->setOffset(0);
51     $c->setLimit(0);
52
53     if (($this->getPage() == 0 || $this->getMaxPerPage() == 0))
54     {
55       $this->setLastPage(0);
56     }
57     else
58     {
59       $this->setLastPage(ceil($this->getNbResults() / $this->getMaxPerPage()));
60
61       $offset = ($this->getPage() - 1) * $this->getMaxPerPage();
62       $c->setOffset($offset);
63
64       if ($hasMaxRecordLimit)
65       {
66         $maxRecordLimit = $maxRecordLimit - $offset;
67         if ($maxRecordLimit > $this->getMaxPerPage())
68         {
69           $c->setLimit($this->getMaxPerPage());
70         }
71         else
72         {
73           $c->setLimit($maxRecordLimit);
74         }
75       }
76       else
77       {
78         $c->setLimit($this->getMaxPerPage());
79       }
80     }
81   }
82
83   protected function retrieveObject($offset)
84   {
85     $cForRetrieve = clone $this->getCriteria();
86     $cForRetrieve->setOffset($offset - 1);
87     $cForRetrieve->setLimit(1);
88
89     $results = call_user_func(array($this->getClassPeer(), $this->getPeerMethod()), $cForRetrieve);
90
91     return is_array($results) && isset($results[0]) ? $results[0] : null;
92   }
93
94   public function getResults()
95   {
96     $c = $this->getCriteria();
97
98     return call_user_func(array($this->getClassPeer(), $this->getPeerMethod()), $c);
99   }
100
101   public function getPeerMethod()
102   {
103     return $this->peer_method_name;
104   }
105
106   public function setPeerMethod($peer_method_name)
107   {
108     $this->peer_method_name = $peer_method_name;
109   }
110
111   public function getPeerCountMethod()
112   {
113     return $this->peer_count_method_name;
114   }
115
116   public function setPeerCountMethod($peer_count_method_name)
117   {
118     $this->peer_count_method_name = $peer_count_method_name;
119   }
120  
121   public function getClassPeer()
122   {
123     return $this->class.'Peer';
124   }
125
126   public function getCriteria()
127   {
128     return $this->criteria;
129   }
130
131   public function setCriteria($c)
132   {
133     $this->criteria = $c;
134   }
135 }
136
Note: See TracBrowser for help on using the browser.