1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Pagerfanta package. |
5
|
|
|
* |
6
|
|
|
* (c) Tugrul Topuz <[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
|
|
|
|
13
|
|
|
namespace Pagerfanta\Adapter; |
14
|
|
|
|
15
|
|
|
use Doctrine\DBAL\Query\QueryBuilder; |
16
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
17
|
|
|
use Doctrine\ORM\Query\ResultSetMapping; |
18
|
|
|
use Pagerfanta\Adapter\AdapterInterface; |
19
|
|
|
use Pagerfanta\Exception\InvalidArgumentException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* DoctrineORMNativeQueryAdapter. |
23
|
|
|
* |
24
|
|
|
* @author Tugrul Topuz <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class DoctrineORMNativeQueryAdapter implements AdapterInterface |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ResultSetMapping |
31
|
|
|
*/ |
32
|
|
|
protected $resultSetMapping; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var EntityManagerInterface |
37
|
|
|
*/ |
38
|
|
|
protected $entityManager; |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var QueryBuilder |
43
|
|
|
*/ |
44
|
|
|
protected $queryBuilder; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var callable |
48
|
|
|
*/ |
49
|
|
|
protected $countQueryBuilderModifier; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Constructor. |
53
|
|
|
* |
54
|
|
|
* @param QueryBuilder $queryBuilder A DBAL query builder. |
55
|
|
|
* @param EntityManagerInterface $entityManager To create NativeQuery instance |
56
|
|
|
* @param ResultSetMapping $resultSetMapping ORM Mapping |
57
|
|
|
* @param callable $countQueryBuilderModifier A callable to modifier the query builder to count. |
58
|
|
|
*/ |
59
|
4 |
|
public function __construct(QueryBuilder $queryBuilder, EntityManagerInterface $entityManager, |
60
|
|
|
ResultSetMapping $resultSetMapping, $countQueryBuilderModifier) |
61
|
|
|
{ |
62
|
4 |
|
if ($queryBuilder->getType() !== QueryBuilder::SELECT) { |
63
|
|
|
throw new InvalidArgumentException('Only SELECT queries can be paginated.'); |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
if (!is_callable($countQueryBuilderModifier)) { |
67
|
|
|
throw new InvalidArgumentException('The count query builder modifier must be a callable.'); |
68
|
|
|
} |
69
|
|
|
|
70
|
4 |
|
$this->queryBuilder = clone $queryBuilder; |
71
|
4 |
|
$this->entityManager = $entityManager; |
72
|
4 |
|
$this->resultSetMapping = $resultSetMapping; |
73
|
4 |
|
$this->countQueryBuilderModifier = $countQueryBuilderModifier; |
74
|
4 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
2 |
|
public function getNbResults() |
80
|
|
|
{ |
81
|
2 |
|
$qb = $this->prepareCountQueryBuilder(); |
82
|
2 |
|
$result = $qb->execute()->fetchColumn(); |
83
|
|
|
|
84
|
2 |
|
return (int) $result; |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
protected function prepareCountQueryBuilder() |
88
|
|
|
{ |
89
|
2 |
|
$qb = clone $this->queryBuilder; |
90
|
2 |
|
call_user_func($this->countQueryBuilderModifier, $qb); |
91
|
|
|
|
92
|
2 |
|
return $qb; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
2 |
|
public function getSlice($offset, $length) |
99
|
|
|
{ |
100
|
2 |
|
$qb = clone $this->queryBuilder; |
101
|
|
|
|
102
|
2 |
|
$qb->setMaxResults($length) |
103
|
2 |
|
->setFirstResult($offset); |
104
|
|
|
|
105
|
2 |
|
$nativeQuery = $this->entityManager->createNativeQuery($qb->getSQL(), $this->resultSetMapping); |
106
|
2 |
|
$nativeQuery->setParameters($qb->getParameters()); |
107
|
2 |
|
return $nativeQuery->getResult(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|