Development

/branches/1.2/lib/plugins/sfDoctrinePlugin/lib/pager/sfDoctrinePager.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/plugins/sfDoctrinePlugin/lib/pager/sfDoctrinePager.class.php

Revision 19265, 3.8 kB (checked in by Jonathan.Wage, 5 years ago)

[1.2, 1.3] Fixes issue with sfDoctrinePager::getResults() not allowing Doctrine hydration type constants (closes #6612)

  • Property svn:keywords set to Id Date Author Revision HeadURL
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6  * (c) Jonathan H. Wage <jonwage@gmail.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 /**
13  * sfDoctrine pager class
14  *
15  * @package    sfDoctrinePlugin
16  * @author     Jonathan H. Wage <jonwage@gmail.com>
17  * @version    SVN: $Id$
18  */
19 class sfDoctrinePager extends sfPager implements Serializable
20 {
21   protected
22     $query                 = null,
23     $tableMethodName       = null,
24     $tableMethodCalled     = false;
25
26   /**
27    * Get the name of the table method used to retrieve the query object for the pager
28    *
29    * @return string $tableMethodName
30    */
31   public function getTableMethod()
32   {
33     return $this->tableMethodName;
34   }
35
36   /**
37    * Set the name of the table method used to retrieve the query object for the pager
38    *
39    * @param string $tableMethodName
40    * @return void
41    */
42   public function setTableMethod($tableMethodName)
43   {
44     $this->tableMethodName = $tableMethodName;
45   }
46
47   /**
48    * Serialize the pager object
49    *
50    * @return string $serialized
51    */
52   public function serialize()
53   {
54     $vars = get_object_vars($this);
55     unset($vars['query']);
56     return serialize($vars);
57   }
58
59   /**
60    * Unserialize a pager object
61    *
62    * @param string $serialized
63    * @return void
64    */
65   public function unserialize($serialized)
66   {
67     $array = unserialize($serialized);
68
69     foreach($array as $name => $values)
70     {
71       $this->$name = $values;
72     }
73   }
74
75   public function getCountQuery()
76   {
77     $q = clone $this->getQuery()
78       ->offset(0)
79       ->limit(0);
80
81     return $q;
82   }
83
84   /**
85    * Initialize the pager instance and prepare it to be used for rendering
86    *
87    * @return void
88    */
89   public function init()
90   {
91     $countQuery = $this->getCountQuery();
92     $count = $countQuery->count();
93
94     $this->setNbResults($count);
95
96     $p = $this->getQuery();
97     $p->offset(0);
98     $p->limit(0);
99     if ($this->getPage() == 0 || $this->getMaxPerPage() == 0 || $this->getNbResults() == 0)
100     {
101       $this->setLastPage(0);
102     }
103     else
104     {
105       $offset = ($this->getPage() - 1) * $this->getMaxPerPage();
106
107       $this->setLastPage(ceil($this->getNbResults() / $this->getMaxPerPage()));
108
109       $p->offset($offset);
110       $p->limit($this->getMaxPerPage());
111     }
112   }
113
114   /**
115    * Get the query for the pager
116    *
117    * @return Doctrine_Query $query
118    */
119   public function getQuery()
120   {
121     if (!$this->tableMethodCalled && $this->tableMethodName)
122     {
123       $method = $this->tableMethodName;
124       $this->query = Doctrine::getTable($this->getClass())->$method($this->query);
125       $this->tableMethodCalled = true;
126     } else if (!$this->query) {
127       $this->query = Doctrine::getTable($this->getClass())->createQuery();
128     }
129     return $this->query;
130   }
131
132   /**
133    * Set query object for the pager
134    *
135    * @param Doctrine_Query $query
136    * @return void
137    */
138   public function setQuery($query)
139   {
140     $this->query = $query;
141   }
142
143   /**
144    * Retrieve the object for a certain offset
145    *
146    * @param integer $offset
147    * @return Doctrine_Record $record
148    */
149   protected function retrieveObject($offset)
150   {
151     $cForRetrieve = clone $this->getQuery();
152     $cForRetrieve->offset($offset - 1);
153     $cForRetrieve->limit(1);
154
155     $results = $cForRetrieve->execute();
156
157     return $results[0];
158   }
159
160   /**
161    * Get all the results for the pager instance
162    *
163    * @param integer $hydrationMode Doctrine::HYDRATE_* constants
164    * @return mixed Doctrine_Collection/array
165    */
166   public function getResults($hydrationMode = Doctrine::HYDRATE_RECORD)
167   {
168     $p = $this->getQuery();
169
170     if ($hydrationMode == 'array')
171     {
172       $hydrationMode = Doctrine::HYDRATE_ARRAY;
173     }
174
175     return $p->execute(array(), $hydrationMode);
176   }
177 }
Note: See TracBrowser for help on using the browser.