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