1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * Doctrine ORM |
---|
5 | * |
---|
6 | * LICENSE |
---|
7 | * |
---|
8 | * This source file is subject to the new BSD license that is bundled |
---|
9 | * with this package in the file LICENSE. This license can also be viewed |
---|
10 | * at http://hobodave.com/license.txt |
---|
11 | * |
---|
12 | * @category DoctrineExtensions |
---|
13 | * @package DoctrineExtensions\Paginate |
---|
14 | * @author David Abdemoulaie <dave@hobodave.com> |
---|
15 | * @copyright Copyright (c) 2010 David Abdemoulaie (http://hobodave.com/) |
---|
16 | * @license http://hobodave.com/license.txt New BSD License |
---|
17 | */ |
---|
18 | |
---|
19 | namespace Doctrine\ORM\Tools\Pagination; |
---|
20 | |
---|
21 | use Doctrine\DBAL\Types\Type, |
---|
22 | Doctrine\ORM\Query\TreeWalkerAdapter, |
---|
23 | Doctrine\ORM\Query\AST\SelectStatement, |
---|
24 | Doctrine\ORM\Query\AST\SelectExpression, |
---|
25 | Doctrine\ORM\Query\AST\PathExpression, |
---|
26 | Doctrine\ORM\Query\AST\AggregateExpression; |
---|
27 | |
---|
28 | /** |
---|
29 | * Replaces the selectClause of the AST with a SELECT DISTINCT root.id equivalent |
---|
30 | * |
---|
31 | * @category DoctrineExtensions |
---|
32 | * @package DoctrineExtensions\Paginate |
---|
33 | * @author David Abdemoulaie <dave@hobodave.com> |
---|
34 | * @copyright Copyright (c) 2010 David Abdemoulaie (http://hobodave.com/) |
---|
35 | * @license http://hobodave.com/license.txt New BSD License |
---|
36 | */ |
---|
37 | class LimitSubqueryWalker extends TreeWalkerAdapter |
---|
38 | { |
---|
39 | /** |
---|
40 | * ID type hint |
---|
41 | */ |
---|
42 | const IDENTIFIER_TYPE = 'doctrine_paginator.id.type'; |
---|
43 | |
---|
44 | /** |
---|
45 | * @var int Counter for generating unique order column aliases |
---|
46 | */ |
---|
47 | private $_aliasCounter = 0; |
---|
48 | |
---|
49 | /** |
---|
50 | * Walks down a SelectStatement AST node, modifying it to retrieve DISTINCT ids |
---|
51 | * of the root Entity |
---|
52 | * |
---|
53 | * @param SelectStatement $AST |
---|
54 | * @return void |
---|
55 | */ |
---|
56 | public function walkSelectStatement(SelectStatement $AST) |
---|
57 | { |
---|
58 | $parent = null; |
---|
59 | $parentName = null; |
---|
60 | $selectExpressions = array(); |
---|
61 | |
---|
62 | foreach ($this->_getQueryComponents() AS $dqlAlias => $qComp) { |
---|
63 | // preserve mixed data in query for ordering |
---|
64 | if (isset($qComp['resultVariable'])) { |
---|
65 | $selectExpressions[] = new SelectExpression($qComp['resultVariable'], $dqlAlias); |
---|
66 | continue; |
---|
67 | } |
---|
68 | |
---|
69 | if ($qComp['parent'] === null && $qComp['nestingLevel'] == 0) { |
---|
70 | $parent = $qComp; |
---|
71 | $parentName = $dqlAlias; |
---|
72 | continue; |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | $identifier = $parent['metadata']->getSingleIdentifierFieldName(); |
---|
77 | $this->_getQuery()->setHint( |
---|
78 | self::IDENTIFIER_TYPE, |
---|
79 | Type::getType($parent['metadata']->getTypeOfField($identifier)) |
---|
80 | ); |
---|
81 | |
---|
82 | $pathExpression = new PathExpression( |
---|
83 | PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, |
---|
84 | $parentName, |
---|
85 | $identifier |
---|
86 | ); |
---|
87 | $pathExpression->type = PathExpression::TYPE_STATE_FIELD; |
---|
88 | |
---|
89 | array_unshift($selectExpressions, new SelectExpression($pathExpression, '_dctrn_id')); |
---|
90 | $AST->selectClause->selectExpressions = $selectExpressions; |
---|
91 | |
---|
92 | if (isset($AST->orderByClause)) { |
---|
93 | foreach ($AST->orderByClause->orderByItems as $item) { |
---|
94 | if ($item->expression instanceof PathExpression) { |
---|
95 | $pathExpression = new PathExpression( |
---|
96 | PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, |
---|
97 | $item->expression->identificationVariable, |
---|
98 | $item->expression->field |
---|
99 | ); |
---|
100 | $pathExpression->type = PathExpression::TYPE_STATE_FIELD; |
---|
101 | $AST->selectClause->selectExpressions[] = new SelectExpression( |
---|
102 | $pathExpression, |
---|
103 | '_dctrn_ord' . $this->_aliasCounter++ |
---|
104 | ); |
---|
105 | } |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | $AST->selectClause->isDistinct = true; |
---|
110 | } |
---|
111 | |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | |
---|