Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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
 * @subpackage pager
17
 * @author     Jonathan H. Wage <jonwage@gmail.com>
18
 * @version    SVN: $Id: sfDoctrinePager.class.php 28897 2010-03-30 20:30:24Z Jonathan.Wage $
19
 */
20
class sfDoctrinePager extends sfPager implements Serializable
21
{
22
  protected
23
    $query             = null,
24
    $tableMethodName   = null,
25
    $tableMethodCalled = false;
26
 
27
  /**
28
   * Get the name of the table method used to retrieve the query object for the pager
29
   *
30
   * @return string $tableMethodName
31
   */
32
  public function getTableMethod()
33
  {
34
    return $this->tableMethodName;
35
  }
36
 
37
  /**
38
   * Set the name of the table method used to retrieve the query object for the pager
39
   *
40
   * @param string $tableMethodName
41
   * @return void
42
   */
43
  public function setTableMethod($tableMethodName)
44
  {
45
    $this->tableMethodName = $tableMethodName;
46
  }
47
 
48
  /**
49
   * Serialize the pager object
50
   *
51
   * @return string $serialized
52
   */
53
  public function serialize()
54
  {
55
    $vars = get_object_vars($this);
56
    unset($vars['query']);
57
    return serialize($vars);
58
  }
59
 
60
  /**
61
   * Unserialize a pager object
62
   *
63
   * @param string $serialized
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
    $this->tableMethodCalled = false;
75
  }
76
 
77
  /**
78
   * Returns a query for counting the total results.
79
   *
80
   * @return Doctrine_Query
81
   */
82
  public function getCountQuery()
83
  {
84
    $query = clone $this->getQuery();
85
    $query
86
      ->offset(0)
87
      ->limit(0)
88
    ;
89
 
90
    return $query;
91
  }
92
 
93
  /**
94
   * @see sfPager
95
   */
96
  public function init()
97
  {
98
    $this->resetIterator();
99
 
100
    $countQuery = $this->getCountQuery();
101
    $count = $countQuery->count();
102
 
103
    $this->setNbResults($count);
104
 
105
    $query = $this->getQuery();
106
    $query
107
      ->offset(0)
108
      ->limit(0)
109
    ;
110
 
111
    if (0 == $this->getPage() || 0 == $this->getMaxPerPage() || 0 == $this->getNbResults())
112
    {
113
      $this->setLastPage(0);
114
    }
115
    else
116
    {
117
      $offset = ($this->getPage() - 1) * $this->getMaxPerPage();
118
 
119
      $this->setLastPage(ceil($this->getNbResults() / $this->getMaxPerPage()));
120
 
121
      $query
122
        ->offset($offset)
123
        ->limit($this->getMaxPerPage())
124
      ;
125
    }
126
  }
127
 
128
  /**
129
   * Get the query for the pager.
130
   *
131
   * @return Doctrine_Query
132
   */
133
  public function getQuery()
134
  {
135
    if (!$this->tableMethodCalled && $this->tableMethodName)
136
    {
137
      $method = $this->tableMethodName;
138
      $this->query = Doctrine_Core::getTable($this->getClass())->$method($this->query);
139
      $this->tableMethodCalled = true;
140
    }
141
    else if (!$this->query)
142
    {
143
      $this->query = Doctrine_Core::getTable($this->getClass())->createQuery();
144
    }
145
 
146
    return $this->query;
147
  }
148
 
149
  /**
150
   * Set query object for the pager
151
   *
152
   * @param Doctrine_Query $query
153
   */
154
  public function setQuery($query)
155
  {
156
    $this->query = $query;
157
  }
158
 
159
  /**
160
   * Retrieve the object for a certain offset
161
   *
162
   * @param integer $offset
163
   *
164
   * @return Doctrine_Record
165
   */
166
  protected function retrieveObject($offset)
167
  {
168
    $queryForRetrieve = clone $this->getQuery();
169
    $queryForRetrieve
170
      ->offset($offset - 1)
171
      ->limit(1)
172
    ;
173
 
174
    $results = $queryForRetrieve->execute();
175
 
176
    return $results[0];
177
  }
178
 
179
  /**
180
   * Get all the results for the pager instance
181
   *
182
   * @param mixed $hydrationMode A hydration mode identifier
183
   *
184
   * @return Doctrine_Collection|array
185
   */
186
  public function getResults($hydrationMode = null)
187
  {
188
    return $this->getQuery()->execute(array(), $hydrationMode);
189
  }
190
 
191
  /**
192
   * @see sfPager
193
   */
194
  protected function initializeIterator()
195
  {
196
    parent::initializeIterator();
197
 
198
    if ($this->results instanceof Doctrine_Collection)
199
    {
200
      $this->results = $this->results->getData();
201
    }
202
  }
203
}