This repository has been archived by the owner on Jul 22, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
Pager.php
73 lines (65 loc) · 2.06 KB
/
Pager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\DoctrinePHPCRAdminBundle\Datagrid;
use Doctrine\ODM\PHPCR\Query\Query as PHPCRQuery;
use Sonata\AdminBundle\Datagrid\Pager as BasePager;
/**
* Doctrine pager class.
*
* @author Jonathan H. Wage <jonwage@gmail.com>
* @author Nacho Martin <nitram.ohcan@gmail.com>
*/
class Pager extends BasePager
{
/**
* Returns a query for counting the total results.
*
* @return int
*/
public function computeNbResult()
{
return count($this->getQuery()->execute([], PHPCRQuery::HYDRATE_PHPCR));
}
/**
* Get all the results for the pager instance.
*
* @param mixed $hydrationMode A hydration mode identifier
*
* @return array
*/
public function getResults($hydrationMode = null)
{
return $this->getQuery()->execute([], $hydrationMode);
}
/**
* Initializes the pager setting the offset and maxResults in ProxyQuery
* and obtaining the total number of pages.
*
* @throws \RuntimeException the QueryBuilder is uninitialized
*/
public function init()
{
if (!$this->getQuery()) {
throw new \RuntimeException('Uninitialized QueryBuilder');
}
$this->resetIterator();
$this->setNbResults($this->computeNbResult());
if (0 == $this->getPage() || 0 == $this->getMaxPerPage() || 0 == $this->getNbResults()) {
$this->setLastPage(0);
$this->getQuery()->setFirstResult(0);
$this->getQuery()->setMaxResults(0);
} else {
$offset = ($this->getPage() - 1) * $this->getMaxPerPage();
$this->setLastPage(ceil($this->getNbResults() / $this->getMaxPerPage()));
$this->getQuery()->setFirstResult($offset);
$this->getQuery()->setMaxResults($this->getMaxPerPage());
}
}
}