1 | <?php |
||
53 | abstract class QueryObject extends Nette\Object implements Kdyby\Persistence\Query |
||
54 | { |
||
55 | |||
56 | /** |
||
57 | * @var array |
||
58 | */ |
||
59 | public $onPostFetch = []; |
||
60 | |||
61 | /** |
||
62 | * @var \Doctrine\ORM\Query |
||
63 | */ |
||
64 | private $lastQuery; |
||
65 | |||
66 | /** |
||
67 | * @var \Kdyby\Doctrine\ResultSet |
||
68 | */ |
||
69 | private $lastResult; |
||
70 | |||
71 | |||
72 | |||
73 | /** |
||
74 | */ |
||
75 | public function __construct() |
||
79 | |||
80 | |||
81 | |||
82 | /** |
||
83 | * @param \Kdyby\Persistence\Queryable $repository |
||
84 | * @return \Doctrine\ORM\Query|\Doctrine\ORM\QueryBuilder |
||
85 | */ |
||
86 | protected abstract function doCreateQuery(Kdyby\Persistence\Queryable $repository); |
||
87 | |||
88 | |||
89 | |||
90 | /** |
||
91 | * @param \Kdyby\Persistence\Queryable $repository |
||
92 | * |
||
93 | * @throws UnexpectedValueException |
||
94 | * @return \Doctrine\ORM\Query |
||
95 | */ |
||
96 | protected function getQuery(Queryable $repository) |
||
110 | |||
111 | |||
112 | |||
113 | /** |
||
114 | * @param Queryable $repository |
||
115 | * @return \Doctrine\ORM\Query|\Doctrine\ORM\QueryBuilder |
||
116 | */ |
||
117 | protected function doCreateCountQuery(Queryable $repository) |
||
121 | |||
122 | |||
123 | |||
124 | /** |
||
125 | * @param \Kdyby\Persistence\Queryable $repository |
||
126 | * @param ResultSet $resultSet |
||
127 | * @param \Doctrine\ORM\Tools\Pagination\Paginator $paginatedQuery |
||
128 | * @return integer |
||
129 | */ |
||
130 | public function count(Queryable $repository, ResultSet $resultSet = NULL, Paginator $paginatedQuery = NULL) |
||
131 | { |
||
132 | if ($query = $this->doCreateCountQuery($repository)) { |
||
133 | return $this->toQuery($query)->getSingleScalarResult(); |
||
134 | } |
||
135 | |||
136 | if ($this->lastQuery && $this->lastQuery instanceof NativeQueryWrapper) { |
||
137 | $class = get_called_class(); |
||
138 | throw new NotSupportedException("You must implement your own count query in $class::doCreateCountQuery(), Paginator from Doctrine doesn't support NativeQueries."); |
||
139 | } |
||
140 | |||
141 | if ($paginatedQuery !== NULL) { |
||
142 | return $paginatedQuery->count(); |
||
143 | } |
||
144 | |||
145 | $query = $this->getQuery($repository) |
||
146 | ->setFirstResult(NULL) |
||
147 | ->setMaxResults(NULL); |
||
148 | |||
149 | $paginatedQuery = new Paginator($query, $resultSet ? $resultSet->getFetchJoinCollection() : TRUE); |
||
150 | $paginatedQuery->setUseOutputWalkers($resultSet ? $resultSet->getUseOutputWalkers() : NULL); |
||
151 | |||
152 | return $paginatedQuery->count(); |
||
153 | } |
||
154 | |||
155 | |||
156 | |||
157 | /** |
||
158 | * @param \Kdyby\Persistence\Queryable $repository |
||
159 | * @param int $hydrationMode |
||
160 | * |
||
161 | * @return \Kdyby\Doctrine\ResultSet|array |
||
162 | */ |
||
163 | public function fetch(Queryable $repository, $hydrationMode = AbstractQuery::HYDRATE_OBJECT) |
||
164 | { |
||
165 | $query = $this->getQuery($repository) |
||
166 | ->setFirstResult(NULL) |
||
167 | ->setMaxResults(NULL); |
||
168 | |||
169 | return $hydrationMode !== AbstractQuery::HYDRATE_OBJECT |
||
170 | ? $query->execute(NULL, $hydrationMode) |
||
171 | : $this->lastResult; |
||
172 | } |
||
173 | |||
174 | |||
175 | |||
176 | /** |
||
177 | * If You encounter a problem with the LIMIT 1 here, |
||
178 | * you should instead of fetching toMany relations just use postFetch. |
||
179 | * |
||
180 | * And if you really really need to hack it, just override this method and remove the limit. |
||
181 | * |
||
182 | * @param \Kdyby\Persistence\Queryable $repository |
||
183 | * @return object |
||
184 | */ |
||
185 | public function fetchOne(Queryable $repository) |
||
186 | { |
||
187 | $query = $this->getQuery($repository) |
||
188 | ->setFirstResult(NULL) |
||
189 | ->setMaxResults(1); |
||
190 | |||
191 | return $query->getSingleResult(); |
||
192 | } |
||
193 | |||
194 | |||
195 | |||
196 | /** |
||
197 | * @param \Kdyby\Persistence\Queryable $repository |
||
198 | * @param \Iterator $iterator |
||
199 | * @return void |
||
200 | */ |
||
201 | public function postFetch(Queryable $repository, \Iterator $iterator) |
||
205 | |||
206 | |||
207 | |||
208 | /** |
||
209 | * @internal For Debugging purposes only! |
||
210 | * @return \Doctrine\ORM\Query |
||
211 | */ |
||
212 | public function getLastQuery() |
||
216 | |||
217 | |||
218 | |||
219 | private function toQuery($query) |
||
241 | |||
242 | } |
||
243 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.