1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata Project package. |
5
|
|
|
* |
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
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
|
|
|
namespace Sonata\DoctrineORMAdminBundle\Datagrid; |
13
|
|
|
|
14
|
|
|
use Doctrine\ORM\Query; |
15
|
|
|
use Doctrine\ORM\Tools\Pagination\CountWalker; |
16
|
|
|
use Sonata\AdminBundle\Datagrid\Pager as BasePager; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Doctrine pager class. |
20
|
|
|
* |
21
|
|
|
* @author Jonathan H. Wage <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class Pager extends BasePager |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* NEXT_MAJOR: remove this property. |
27
|
|
|
* |
28
|
|
|
* @deprecated This property is deprecated since version 2.4 and will be removed in 3.0 |
29
|
|
|
*/ |
30
|
|
|
protected $queryBuilder = null; |
31
|
|
|
|
32
|
|
|
public function computeNbResult() |
33
|
|
|
{ |
34
|
|
|
$countQuery = clone $this->getQuery(); |
35
|
|
|
|
36
|
|
|
if (count($this->getParameters()) > 0) { |
37
|
|
|
$countQuery->setParameters($this->getParameters()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$query = $countQuery->getQuery(); |
41
|
|
|
$isDistinct = $countQuery instanceof ProxyQuery ? $countQuery->isDistinct() : true; |
42
|
|
|
$query->setHint(CountWalker::HINT_DISTINCT, $isDistinct); |
43
|
|
|
$this->appendTreeWalker($query, CountWalker::class); |
44
|
|
|
|
45
|
|
|
return $query->getSingleScalarResult(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getResults($hydrationMode = Query::HYDRATE_OBJECT) |
49
|
|
|
{ |
50
|
|
|
return $this->getQuery()->execute([], $hydrationMode); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getQuery() |
54
|
|
|
{ |
55
|
|
|
return $this->query; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function init() |
59
|
|
|
{ |
60
|
|
|
$this->resetIterator(); |
61
|
|
|
|
62
|
|
|
$this->setNbResults($this->computeNbResult()); |
63
|
|
|
|
64
|
|
|
$this->getQuery()->setFirstResult(null); |
65
|
|
|
$this->getQuery()->setMaxResults(null); |
66
|
|
|
|
67
|
|
|
if (count($this->getParameters()) > 0) { |
68
|
|
|
$this->getQuery()->setParameters($this->getParameters()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (0 == $this->getPage() || 0 == $this->getMaxPerPage() || 0 == $this->getNbResults()) { |
72
|
|
|
$this->setLastPage(0); |
73
|
|
|
} else { |
74
|
|
|
$offset = ($this->getPage() - 1) * $this->getMaxPerPage(); |
75
|
|
|
|
76
|
|
|
$this->setLastPage(ceil($this->getNbResults() / $this->getMaxPerPage())); |
77
|
|
|
|
78
|
|
|
$this->getQuery()->setFirstResult($offset); |
79
|
|
|
$this->getQuery()->setMaxResults($this->getMaxPerPage()); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Appends a custom tree walker to the tree walkers hint. |
85
|
|
|
* |
86
|
|
|
* @param Query $query |
87
|
|
|
* @param string $walkerClass |
88
|
|
|
*/ |
89
|
|
|
private function appendTreeWalker(Query $query, $walkerClass) |
90
|
|
|
{ |
91
|
|
|
$hints = $query->getHint(Query::HINT_CUSTOM_TREE_WALKERS); |
92
|
|
|
|
93
|
|
|
if ($hints === false) { |
94
|
|
|
$hints = []; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$hints[] = $walkerClass; |
98
|
|
|
$query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, $hints); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|