|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This is just a proxy to detect if we can use the "fast" Pagination |
|
4
|
|
|
* or if we use the "safe" variant by Doctrine2. |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace ZfcDatagrid\DataSource\Doctrine2; |
|
7
|
|
|
|
|
8
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
9
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator as Doctrine2Paginator; |
|
10
|
|
|
use Zend\Paginator\Adapter\AdapterInterface; |
|
11
|
|
|
use ZfcDatagrid\DataSource\Doctrine2\PaginatorFast as ZfcDatagridPaginator; |
|
12
|
|
|
|
|
13
|
|
|
class Paginator implements AdapterInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var QueryBuilder |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $qb = null; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Total item count. |
|
22
|
|
|
* |
|
23
|
|
|
* @var int |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $rowCount = null; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var \Doctrine\ORM\Tools\Pagination\Paginator |
|
29
|
|
|
*/ |
|
30
|
|
|
private $paginator; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param QueryBuilder $qb |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(QueryBuilder $qb) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->qb = $qb; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return \Doctrine\ORM\QueryBuilder |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getQueryBuilder() |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->qb; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Test which pagination solution to use. |
|
50
|
|
|
* |
|
51
|
|
|
* @return bool |
|
52
|
|
|
*/ |
|
53
|
|
|
private function useCustomPaginator() |
|
54
|
|
|
{ |
|
55
|
|
|
$qb = $this->getQueryBuilder(); |
|
56
|
|
|
$parts = $qb->getDQLParts(); |
|
57
|
|
|
|
|
58
|
|
|
if ($parts['having'] !== null || true === $parts['distinct']) { |
|
59
|
|
|
// never tried having in such queries... |
|
60
|
|
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// @todo maybe more detection needed :-/ |
|
64
|
|
|
return true; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return Doctrine2Paginator|ZfcDatagridPaginator |
|
69
|
|
|
*/ |
|
70
|
|
|
private function getPaginator() |
|
71
|
|
|
{ |
|
72
|
|
|
if ($this->paginator !== null) { |
|
73
|
|
|
return $this->paginator; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if ($this->useCustomPaginator() === true) { |
|
77
|
|
|
$this->paginator = new ZfcDatagridPaginator($this->getQueryBuilder()); |
|
|
|
|
|
|
78
|
|
|
} else { |
|
79
|
|
|
// Doctrine2Paginator as fallback...they are using 3 queries |
|
80
|
|
|
$this->paginator = new Doctrine2Paginator($this->getQueryBuilder()); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $this->paginator; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Returns an array of items for a page. |
|
88
|
|
|
* |
|
89
|
|
|
* @param int $offset |
|
90
|
|
|
* @param int $itemCountPerPage |
|
91
|
|
|
* |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getItems($offset, $itemCountPerPage) |
|
95
|
|
|
{ |
|
96
|
|
|
$paginator = $this->getPaginator(); |
|
97
|
|
|
if ($paginator instanceof Doctrine2Paginator) { |
|
98
|
|
|
$this->getQueryBuilder() |
|
99
|
|
|
->setFirstResult($offset) |
|
100
|
|
|
->setMaxResults($itemCountPerPage); |
|
101
|
|
|
|
|
102
|
|
|
return $paginator->getIterator()->getArrayCopy(); |
|
|
|
|
|
|
103
|
|
|
} else { |
|
104
|
|
|
return $paginator->getItems($offset, $itemCountPerPage); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Returns the total number of rows in the result set. |
|
110
|
|
|
* |
|
111
|
|
|
* @return int |
|
112
|
|
|
*/ |
|
113
|
|
|
public function count() |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->getPaginator()->count(); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..