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\CountOutputWalker; |
16
|
|
|
use Sonata\AdminBundle\Datagrid\Pager as BasePager; |
17
|
|
|
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; |
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 = $this->getClonedQuery()->getQuery(); |
36
|
|
|
|
37
|
|
|
$platform = $countQuery->getEntityManager()->getConnection()->getDatabasePlatform(); |
38
|
|
|
$rsm = new Query\ResultSetMapping(); |
39
|
|
|
$rsm->addScalarResult($platform->getSQLResultCasing('dctrn_count'), 'count'); |
40
|
|
|
$countQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, CountOutputWalker::class); |
41
|
|
|
$countQuery->setResultSetMapping($rsm); |
42
|
|
|
|
43
|
|
|
$countQuery->setFirstResult(null)->setMaxResults(null); |
44
|
|
|
|
45
|
|
|
return $countQuery->getSingleScalarResult(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getResults($hydrationMode = Query::HYDRATE_OBJECT) |
49
|
|
|
{ |
50
|
|
|
return $this->getQuery()->execute([], $hydrationMode); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function init() |
54
|
|
|
{ |
55
|
|
|
$this->resetIterator(); |
56
|
|
|
|
57
|
|
|
$this->setNbResults($this->computeNbResult()); |
58
|
|
|
|
59
|
|
|
$this->getQuery()->setFirstResult(null); |
60
|
|
|
$this->getQuery()->setMaxResults(null); |
61
|
|
|
|
62
|
|
|
if (\count($this->getParameters()) > 0) { |
63
|
|
|
$this->getQuery()->setParameters($this->getParameters()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (0 == $this->getPage() || 0 == $this->getMaxPerPage() || 0 == $this->getNbResults()) { |
67
|
|
|
$this->setLastPage(0); |
68
|
|
|
} else { |
69
|
|
|
$offset = ($this->getPage() - 1) * $this->getMaxPerPage(); |
70
|
|
|
|
71
|
|
|
$this->setLastPage(ceil($this->getNbResults() / $this->getMaxPerPage())); |
72
|
|
|
|
73
|
|
|
$this->getQuery()->setFirstResult($offset); |
74
|
|
|
$this->getQuery()->setMaxResults($this->getMaxPerPage()); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return ProxyQueryInterface |
80
|
|
|
*/ |
81
|
|
|
private function getClonedQuery() |
82
|
|
|
{ |
83
|
|
|
/** @var ProxyQueryInterface $cloneQuery */ |
84
|
|
|
$cloneQuery = clone $this->getQuery(); |
85
|
|
|
|
86
|
|
|
if (\count($this->getParameters()) > 0) { |
87
|
|
|
$countQuery->setParameters($this->getParameters()); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $cloneQuery; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.