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\Query; |
17
|
|
|
use Sonata\AdminBundle\Datagrid\Pager as BasePager; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Doctrine pager class. |
21
|
|
|
* |
22
|
|
|
* @author Jonathan H. Wage <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class Pager extends BasePager |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* NEXT_MAJOR: remove this property. |
28
|
|
|
* |
29
|
|
|
* @deprecated This property is deprecated since version 2.4 and will be removed in 3.0 |
30
|
|
|
*/ |
31
|
|
|
protected $queryBuilder = null; |
32
|
|
|
|
33
|
|
|
public function computeNbResult() |
34
|
|
|
{ |
35
|
|
|
$countQuery = clone $this->getQuery(); |
36
|
|
|
|
37
|
|
|
if (\count($this->getParameters()) > 0) { |
38
|
|
|
$countQuery->setParameters($this->getParameters()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$countQuery->select(sprintf( |
42
|
|
|
'count(%s %s) as cnt', |
43
|
|
|
$countQuery instanceof ProxyQuery && !$countQuery->isDistinct() ? null : 'DISTINCT', |
44
|
|
|
$this->getColumnsForQuery(current($countQuery->getRootAliases())) |
45
|
|
|
)); |
46
|
|
|
|
47
|
|
|
return $countQuery->resetDQLPart('orderBy')->getQuery()->getSingleScalarResult(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getResults($hydrationMode = Query::HYDRATE_OBJECT) |
51
|
|
|
{ |
52
|
|
|
return $this->getQuery()->execute([], $hydrationMode); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getQuery() |
56
|
|
|
{ |
57
|
|
|
return $this->query; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function init(): void |
61
|
|
|
{ |
62
|
|
|
$this->resetIterator(); |
63
|
|
|
|
64
|
|
|
$this->setNbResults($this->computeNbResult()); |
65
|
|
|
|
66
|
|
|
$this->getQuery()->setFirstResult(null); |
67
|
|
|
$this->getQuery()->setMaxResults(null); |
68
|
|
|
|
69
|
|
|
if (\count($this->getParameters()) > 0) { |
70
|
|
|
$this->getQuery()->setParameters($this->getParameters()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (0 == $this->getPage() || 0 == $this->getMaxPerPage() || 0 == $this->getNbResults()) { |
74
|
|
|
$this->setLastPage(0); |
75
|
|
|
} else { |
76
|
|
|
$offset = ($this->getPage() - 1) * $this->getMaxPerPage(); |
77
|
|
|
|
78
|
|
|
$this->setLastPage(ceil($this->getNbResults() / $this->getMaxPerPage())); |
79
|
|
|
|
80
|
|
|
$this->getQuery()->setFirstResult($offset); |
81
|
|
|
$this->getQuery()->setMaxResults($this->getMaxPerPage()); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function getColumnsForQuery(string $rootAlias): string |
86
|
|
|
{ |
87
|
|
|
if (1 === \count($countColumns = $this->getCountColumn())) { |
88
|
|
|
return $rootAlias.'.'.current($countColumns); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$columns = implode(",'-',", array_map(function ($column) use ($rootAlias) { |
92
|
|
|
return "$rootAlias.$column"; |
93
|
|
|
}, $countColumns)); |
94
|
|
|
|
95
|
|
|
return "concat($columns)"; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|