1 | <?php |
||
43 | class RecordSelector extends Component implements \IteratorAggregate, \Countable, PaginatorAwareInterface |
||
44 | { |
||
45 | use SaturateTrait; |
||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | private $class; |
||
51 | |||
52 | /** |
||
53 | * @invisible |
||
54 | * @var ORMInterface |
||
55 | */ |
||
56 | private $orm; |
||
57 | |||
58 | /** |
||
59 | * @var RootLoader |
||
60 | */ |
||
61 | private $loader; |
||
62 | |||
63 | /** |
||
64 | * @param string $class |
||
65 | * @param ORMInterface $orm |
||
66 | */ |
||
67 | public function __construct(string $class, ORMInterface $orm) |
||
78 | |||
79 | /** |
||
80 | * Get associated ORM instance, can be used to create separate query/selection using same |
||
81 | * (nested) memory scope for ORM cache. |
||
82 | * |
||
83 | * @see ORM::selector() |
||
84 | * @return ORMInterface |
||
85 | */ |
||
86 | public function getORM(): ORMInterface |
||
90 | |||
91 | /** |
||
92 | * Get associated class. |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | public function getClass(): string |
||
100 | |||
101 | /** |
||
102 | * Get alias used for primary table. |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | public function getAlias(): string |
||
110 | |||
111 | /** |
||
112 | * Columns to be selected, please note, primary will always be included, DO not include |
||
113 | * column aliases in here, aliases will be added automatically. Creates selector as response. |
||
114 | * |
||
115 | * @param array $columns |
||
116 | * |
||
117 | * @return RecordSelector |
||
118 | */ |
||
119 | public function withColumns(array $columns): self |
||
126 | |||
127 | /** |
||
128 | * Request primary selector loader to pre-load relation name. Any type of loader can be used |
||
129 | * for |
||
130 | * data preloading. ORM loaders by default will select the most efficient way to load related |
||
131 | * data which might include additional select query or left join. Loaded data will |
||
132 | * automatically pre-populate record relations. You can specify nested relations using "." |
||
133 | * separator. |
||
134 | * |
||
135 | * Examples: |
||
136 | * |
||
137 | * //Select users and load their comments (will cast 2 queries, HAS_MANY comments) |
||
138 | * User::find()->with('comments'); |
||
139 | * |
||
140 | * //You can load chain of relations - select user and load their comments and post related to |
||
141 | * //comment |
||
142 | * User::find()->with('comments.post'); |
||
143 | * |
||
144 | * //We can also specify custom where conditions on data loading, let's load only public |
||
145 | * comments. User::find()->load('comments', [ |
||
146 | * 'where' => ['{@}.status' => 'public'] |
||
147 | * ]); |
||
148 | * |
||
149 | * Please note using "{@}" column name, this placeholder is required to prevent collisions and |
||
150 | * it will be automatically replaced with valid table alias of pre-loaded comments table. |
||
151 | * |
||
152 | * //In case where your loaded relation is MANY_TO_MANY you can also specify pivot table |
||
153 | * conditions, |
||
154 | * //let's pre-load all approved user tags, we can use same placeholder for pivot table alias |
||
155 | * User::find()->load('tags', [ |
||
156 | * 'wherePivot' => ['{@}.approved' => true] |
||
157 | * ]); |
||
158 | * |
||
159 | * //In most of cases you don't need to worry about how data was loaded, using external query |
||
160 | * or |
||
161 | * //left join, however if you want to change such behaviour you can force load method to |
||
162 | * INLOAD |
||
163 | * User::find()->load('tags', [ |
||
164 | * 'method' => Loader::INLOAD, |
||
165 | * 'wherePivot' => ['{@}.approved' => true] |
||
166 | * ]); |
||
167 | * |
||
168 | * Attention, you will not be able to correctly paginate in this case and only ORM loaders |
||
169 | * support different loading types. |
||
170 | * |
||
171 | * You can specify multiple loaders using array as first argument. |
||
172 | * |
||
173 | * Example: |
||
174 | * User::find()->load(['posts', 'comments', 'profile']); |
||
175 | * |
||
176 | * Attention, consider disabling entity map if you want to use recursive loading (i.e. |
||
177 | * post.tags.posts), but first think why you even need recursive relation loading. |
||
178 | * |
||
179 | * @see with() |
||
180 | * |
||
181 | * @param string|array $relation |
||
182 | * @param array $options |
||
183 | * |
||
184 | * @return $this|RecordSelector |
||
185 | */ |
||
186 | public function load($relation, array $options = []): self |
||
187 | { |
||
188 | if (is_array($relation)) { |
||
189 | foreach ($relation as $name => $subOption) { |
||
190 | if (is_string($subOption)) { |
||
191 | //Array of relation names |
||
192 | $this->load($subOption, $options); |
||
193 | } else { |
||
194 | //Multiple relations or relation with addition load options |
||
195 | $this->load($name, $subOption + $options); |
||
196 | } |
||
197 | } |
||
198 | |||
199 | return $this; |
||
200 | } |
||
201 | |||
202 | //We are requesting primary loaded to pre-load nested relation |
||
203 | $this->loader->loadRelation($relation, $options); |
||
204 | |||
205 | return $this; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * With method is very similar to load() one, except it will always include related data to |
||
210 | * parent query using INNER JOIN, this method can be applied only to ORM loaders and relations |
||
211 | * using same database as parent record. |
||
212 | * |
||
213 | * Method generally used to filter data based on some relation condition. |
||
214 | * Attention, with() method WILL NOT load relation data, it will only make it accessible in |
||
215 | * query. |
||
216 | * |
||
217 | * By default joined tables will be available in query based on relation name, you can change |
||
218 | * joined table alias using relation option "alias". |
||
219 | * |
||
220 | * Do not forget to set DISTINCT flag while including HAS_MANY and MANY_TO_MANY relations. In |
||
221 | * other scenario you will not able to paginate data well. |
||
222 | * |
||
223 | * Examples: |
||
224 | * |
||
225 | * //Find all users who have comments comments |
||
226 | * User::find()->with('comments'); |
||
227 | * |
||
228 | * //Find all users who have approved comments (we can use comments table alias in where |
||
229 | * statement). |
||
230 | * User::find()->with('comments')->where('comments.approved', true); |
||
231 | * |
||
232 | * //Find all users who have posts which have approved comments |
||
233 | * User::find()->with('posts.comments')->where('posts_comments.approved', true); |
||
234 | * |
||
235 | * //Custom join alias for post comments relation |
||
236 | * $user->with('posts.comments', [ |
||
237 | * 'alias' => 'comments' |
||
238 | * ])->where('comments.approved', true); |
||
239 | * |
||
240 | * //If you joining MANY_TO_MANY relation you will be able to use pivot table used as relation |
||
241 | * name |
||
242 | * //plus "_pivot" postfix. Let's load all users with approved tags. |
||
243 | * $user->with('tags')->where('tags_pivot.approved', true); |
||
244 | * |
||
245 | * //You can also use custom alias for pivot table as well |
||
246 | * User::find()->with('tags', [ |
||
247 | * 'pivotAlias' => 'tags_connection' |
||
248 | * ]) |
||
249 | * ->where('tags_connection.approved', false); |
||
250 | * |
||
251 | * You can safely combine with() and load() methods. |
||
252 | * |
||
253 | * //Load all users with approved comments and pre-load all their comments |
||
254 | * User::find()->with('comments')->where('comments.approved', true) |
||
255 | * ->load('comments'); |
||
256 | * |
||
257 | * //You can also use custom conditions in this case, let's find all users with approved |
||
258 | * comments |
||
259 | * //and pre-load such approved comments |
||
260 | * User::find()->with('comments')->where('comments.approved', true) |
||
261 | * ->load('comments', [ |
||
262 | * 'where' => ['{@}.approved' => true] |
||
263 | * ]); |
||
264 | * |
||
265 | * //As you might notice previous construction will create 2 queries, however we can simplify |
||
266 | * //this construction to use already joined table as source of data for relation via "using" |
||
267 | * //keyword |
||
268 | * User::find()->with('comments') |
||
269 | * ->where('comments.approved', true) |
||
270 | * ->load('comments', ['using' => 'comments']); |
||
271 | * |
||
272 | * //You will get only one query with INNER JOIN, to better understand this example let's use |
||
273 | * //custom alias for comments in with() method. |
||
274 | * User::find()->with('comments', ['alias' => 'commentsR']) |
||
275 | * ->where('commentsR.approved', true) |
||
276 | * ->load('comments', ['using' => 'commentsR']); |
||
277 | * |
||
278 | * @see load() |
||
279 | * |
||
280 | * @param string|array $relation |
||
281 | * @param array $options |
||
282 | * |
||
283 | * @return $this|RecordSelector |
||
284 | */ |
||
285 | public function with($relation, array $options = []): self |
||
286 | { |
||
287 | if (is_array($relation)) { |
||
288 | foreach ($relation as $name => $options) { |
||
289 | if (is_string($options)) { |
||
290 | //Array of relation names |
||
291 | $this->with($options, []); |
||
292 | } else { |
||
293 | //Multiple relations or relation with addition load options |
||
294 | $this->with($name, $options); |
||
295 | } |
||
296 | } |
||
297 | |||
298 | return $this; |
||
299 | } |
||
300 | |||
301 | //Requesting primary loader to join nested relation, will only work for ORM loaders |
||
302 | $this->loader->loadRelation($relation, $options, true); |
||
303 | |||
304 | return $this; |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * Shortcut to where method to set AND condition for parent record primary key. |
||
309 | * |
||
310 | * @param string|int $id |
||
311 | * |
||
312 | * @return RecordSelector |
||
313 | * |
||
314 | * @throws SelectorException |
||
315 | */ |
||
316 | public function wherePK($id): self |
||
331 | |||
332 | /** |
||
333 | * Find one entity or return null. |
||
334 | * |
||
335 | * @param array|null $query |
||
336 | * |
||
337 | * @return EntityInterface|null |
||
338 | */ |
||
339 | public function findOne(array $query = null) |
||
349 | |||
350 | /** |
||
351 | * Fetch all records in a form of array. |
||
352 | * |
||
353 | * @param string $cacheKey |
||
354 | * @param int|\DateInterval $ttl |
||
355 | * @param CacheInterface|null $cache Can be automatically resoled via ORM container scope. |
||
356 | * |
||
357 | * @return RecordInterface[] |
||
358 | */ |
||
359 | public function fetchAll( |
||
366 | |||
367 | /** |
||
368 | * Get RecordIterator (entity iterator) for a requested data. Provide cache key and lifetime in |
||
369 | * order to cache request data. |
||
370 | * |
||
371 | * @param string $cacheKey |
||
372 | * @param int|\DateInterval $ttl |
||
373 | * @param CacheInterface|null $cache Can be automatically resoled via ORM container scope. |
||
374 | * |
||
375 | * @return RecordIterator|RecordInterface[] |
||
376 | */ |
||
377 | public function getIterator( |
||
402 | |||
403 | /** |
||
404 | * Attention, column will be quoted by driver! |
||
405 | * |
||
406 | * @param string|null $column When column is null DISTINCT(PK) will be generated. |
||
407 | * |
||
408 | * @return int |
||
409 | */ |
||
410 | public function count(string $column = null): int |
||
423 | |||
424 | /** |
||
425 | * Query used as basement for relation. |
||
426 | * |
||
427 | * @return SelectQuery |
||
428 | */ |
||
429 | public function initialQuery(): SelectQuery |
||
433 | |||
434 | /** |
||
435 | * Get compiled version of SelectQuery, attentionly only first level query access is allowed. |
||
436 | * |
||
437 | * @return SelectQuery |
||
438 | */ |
||
439 | public function compiledQuery(): SelectQuery |
||
443 | |||
444 | /** |
||
445 | * Load data tree from databases and linked loaders in a form of array. |
||
446 | * |
||
447 | * @param OutputNode $node When empty node will be created automatically by root relation |
||
448 | * loader. |
||
449 | * |
||
450 | * @return array |
||
451 | */ |
||
452 | public function fetchData(OutputNode $node = null): array |
||
462 | |||
463 | /** |
||
464 | * {@inheritdoc} |
||
465 | */ |
||
466 | public function hasPaginator(): bool |
||
470 | |||
471 | /** |
||
472 | * {@inheritdoc} |
||
473 | */ |
||
474 | public function setPaginator(PaginatorInterface $paginator) |
||
478 | |||
479 | /** |
||
480 | * {@inheritdoc} |
||
481 | */ |
||
482 | public function getPaginator(bool $prepare = true): PaginatorInterface |
||
486 | |||
487 | /** |
||
488 | * Bypassing call to primary select query. |
||
489 | * |
||
490 | * @param string $name |
||
491 | * @param $arguments |
||
492 | * |
||
493 | * @return $this|mixed |
||
494 | */ |
||
495 | public function __call(string $name, array $arguments) |
||
511 | |||
512 | /** |
||
513 | * Cloning with loader tree cloning. |
||
514 | * |
||
515 | * @attention at this moment binded query parameters would't be cloned! |
||
516 | */ |
||
517 | public function __clone() |
||
521 | |||
522 | /** |
||
523 | * Remove nested loaders and clean ORM link. |
||
524 | */ |
||
525 | public function __destruct() |
||
530 | |||
531 | /** |
||
532 | * @return \Interop\Container\ContainerInterface|null |
||
533 | */ |
||
534 | protected function iocContainer() |
||
543 | } |