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
|
|
|
private const CONCAT_SEPARATOR = '|'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* NEXT_MAJOR: remove this property. |
30
|
|
|
* |
31
|
|
|
* @deprecated since sonata-project/doctrine-orm-admin-bundle 2.4 and will be removed in 4.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
|
|
|
if (\count($this->getCountColumn()) > 1) { |
44
|
|
|
$root_aliases = current($countQuery->getRootAliases()); |
45
|
|
|
$countQuery->setParameter('concat_separator', self::CONCAT_SEPARATOR); |
46
|
|
|
$columns = []; |
47
|
|
|
|
48
|
|
|
foreach ($this->getCountColumn() as $column) { |
49
|
|
|
if ($columns) { |
|
|
|
|
50
|
|
|
$columns[] = ':concat_separator'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$columns[] = sprintf('%s.%s', $root_aliases, $column); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$countQuery->select(sprintf( |
57
|
|
|
'count(%s concat(%s)) as cnt', |
58
|
|
|
$countQuery instanceof ProxyQuery && !$countQuery->isDistinct() ? null : 'DISTINCT', |
59
|
|
|
implode(',', $columns) |
60
|
|
|
)); |
61
|
|
|
} else { |
62
|
|
|
$countQuery->select(sprintf( |
63
|
|
|
'count(%s %s.%s) as cnt', |
64
|
|
|
$countQuery instanceof ProxyQuery && !$countQuery->isDistinct() ? null : 'DISTINCT', |
65
|
|
|
current($countQuery->getRootAliases()), |
66
|
|
|
current($this->getCountColumn()) |
67
|
|
|
)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return array_sum(array_column( |
71
|
|
|
$countQuery->resetDQLPart('orderBy')->getQuery()->getResult(Query::HYDRATE_SCALAR), |
72
|
|
|
'cnt' |
73
|
|
|
)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getResults($hydrationMode = Query::HYDRATE_OBJECT) |
77
|
|
|
{ |
78
|
|
|
return $this->getQuery()->execute([], $hydrationMode); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getQuery() |
82
|
|
|
{ |
83
|
|
|
return $this->query; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function init() |
87
|
|
|
{ |
88
|
|
|
$this->resetIterator(); |
89
|
|
|
|
90
|
|
|
$this->setNbResults($this->computeNbResult()); |
91
|
|
|
|
92
|
|
|
$this->getQuery()->setFirstResult(null); |
93
|
|
|
$this->getQuery()->setMaxResults(null); |
94
|
|
|
|
95
|
|
|
if (\count($this->getParameters()) > 0) { |
96
|
|
|
$this->getQuery()->setParameters($this->getParameters()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (0 === $this->getPage() || 0 === $this->getMaxPerPage() || 0 === $this->getNbResults()) { |
100
|
|
|
$this->setLastPage(0); |
101
|
|
|
} else { |
102
|
|
|
$offset = ($this->getPage() - 1) * $this->getMaxPerPage(); |
103
|
|
|
|
104
|
|
|
$this->setLastPage((int) ceil($this->getNbResults() / $this->getMaxPerPage())); |
105
|
|
|
|
106
|
|
|
$this->getQuery()->setFirstResult($offset); |
107
|
|
|
$this->getQuery()->setMaxResults($this->getMaxPerPage()); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.