1 | <?php |
||
21 | class ProxyQuery implements ProxyQueryInterface |
||
22 | { |
||
23 | /** |
||
24 | * @var QueryBuilder |
||
25 | */ |
||
26 | protected $queryBuilder; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $sortBy; |
||
32 | |||
33 | /** |
||
34 | * @var mixed |
||
35 | */ |
||
36 | protected $sortOrder; |
||
37 | |||
38 | /** |
||
39 | * @var int |
||
40 | */ |
||
41 | protected $uniqueParameterId; |
||
42 | |||
43 | /** |
||
44 | * @var string[] |
||
45 | */ |
||
46 | protected $entityJoinAliases; |
||
47 | |||
48 | /** |
||
49 | * @param QueryBuilder $queryBuilder |
||
50 | */ |
||
51 | public function __construct($queryBuilder) |
||
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | public function __call($name, $args) |
||
62 | { |
||
63 | return call_user_func_array(array($this->queryBuilder, $name), $args); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | public function __get($name) |
||
70 | { |
||
71 | return $this->queryBuilder->$name; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | public function __clone() |
||
78 | { |
||
79 | $this->queryBuilder = clone $this->queryBuilder; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | public function execute(array $params = array(), $hydrationMode = null) |
||
86 | { |
||
87 | // always clone the original queryBuilder |
||
88 | $queryBuilder = clone $this->queryBuilder; |
||
89 | |||
90 | $rootAlias = current($queryBuilder->getRootAliases()); |
||
91 | |||
92 | // todo : check how doctrine behave, potential SQL injection here ... |
||
93 | if ($this->getSortBy()) { |
||
94 | $sortBy = $this->getSortBy(); |
||
95 | if (strpos($sortBy, '.') === false) { // add the current alias |
||
96 | $sortBy = $rootAlias.'.'.$sortBy; |
||
97 | } |
||
98 | $queryBuilder->addOrderBy($sortBy, $this->getSortOrder()); |
||
99 | } else { |
||
100 | $queryBuilder->resetDQLPart('orderBy'); |
||
101 | } |
||
102 | |||
103 | /* By default, always add a sort on the identifier fields of the first |
||
104 | * used entity in the query, because RDBMS do not guarantee a |
||
105 | * particular order when no ORDER BY clause is specified, or when |
||
106 | * the field used for sorting is not unique. |
||
107 | */ |
||
108 | |||
109 | $identifierFields = $queryBuilder |
||
110 | ->getEntityManager() |
||
111 | ->getMetadataFactory() |
||
112 | ->getMetadataFor(current($queryBuilder->getRootEntities())) |
||
113 | ->getIdentifierFieldNames(); |
||
114 | |||
115 | foreach ($identifierFields as $identifierField) { |
||
116 | $queryBuilder->addOrderBy( |
||
117 | $rootAlias.'.'.$identifierField, |
||
118 | $this->getSortOrder() // reusing the sort order is the most natural way to go |
||
119 | ); |
||
120 | } |
||
121 | |||
122 | return $this->getFixedQueryBuilder($queryBuilder)->getQuery()->execute($params, $hydrationMode); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | public function setSortBy($parentAssociationMappings, $fieldMapping) |
||
129 | { |
||
130 | $alias = $this->entityJoin($parentAssociationMappings); |
||
131 | $this->sortBy = $alias.'.'.$fieldMapping['fieldName']; |
||
132 | |||
133 | return $this; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * {@inheritdoc} |
||
138 | */ |
||
139 | public function getSortBy() |
||
140 | { |
||
141 | return $this->sortBy; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * {@inheritdoc} |
||
146 | */ |
||
147 | public function setSortOrder($sortOrder) |
||
148 | { |
||
149 | $this->sortOrder = $sortOrder; |
||
150 | |||
151 | return $this; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * {@inheritdoc} |
||
156 | */ |
||
157 | public function getSortOrder() |
||
158 | { |
||
159 | return $this->sortOrder; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * {@inheritdoc} |
||
164 | */ |
||
165 | public function getSingleScalarResult() |
||
166 | { |
||
167 | $query = $this->queryBuilder->getQuery(); |
||
168 | |||
169 | return $query->getSingleScalarResult(); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @return mixed |
||
174 | */ |
||
175 | public function getQueryBuilder() |
||
176 | { |
||
177 | return $this->queryBuilder; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * {@inheritdoc} |
||
182 | */ |
||
183 | public function setFirstResult($firstResult) |
||
184 | { |
||
185 | $this->queryBuilder->setFirstResult($firstResult); |
||
186 | |||
187 | return $this; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * {@inheritdoc} |
||
192 | */ |
||
193 | public function getFirstResult() |
||
194 | { |
||
195 | return $this->queryBuilder->getFirstResult(); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | */ |
||
201 | public function setMaxResults($maxResults) |
||
207 | |||
208 | /** |
||
209 | * {@inheritdoc} |
||
210 | */ |
||
211 | public function getMaxResults() |
||
215 | |||
216 | /** |
||
217 | * {@inheritdoc} |
||
218 | */ |
||
219 | public function getUniqueParameterId() |
||
223 | |||
224 | /** |
||
225 | * {@inheritdoc} |
||
226 | */ |
||
227 | public function entityJoin(array $associationMappings) |
||
228 | { |
||
229 | $alias = $this->queryBuilder->getRootAlias(); |
||
|
|||
230 | |||
231 | $newAlias = 's'; |
||
232 | |||
233 | $joinedEntities = $this->queryBuilder->getDQLPart('join'); |
||
234 | |||
235 | foreach ($associationMappings as $associationMapping) { |
||
262 | |||
263 | /** |
||
264 | * This method alters the query to return a clean set of object with a working |
||
265 | * set of Object. |
||
266 | * |
||
267 | * @param QueryBuilder $queryBuilder |
||
268 | * |
||
269 | * @return QueryBuilder |
||
270 | */ |
||
271 | protected function getFixedQueryBuilder(QueryBuilder $queryBuilder) |
||
340 | } |
||
341 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.