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 |
||
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 |
||
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 | * Get RecordIterator (entity iterator) for a requested data. Provide cache key and lifetime in |
||
352 | * order to cache request data. |
||
353 | * |
||
354 | * @param string $cacheKey |
||
355 | * @param int|\DateInterval $ttl |
||
356 | * @param CacheInterface|null $cache Can be automatically resoled via ORM container scope. |
||
357 | * |
||
358 | * @return RecordIterator|RecordInterface[] |
||
359 | */ |
||
360 | public function getIterator( |
||
385 | |||
386 | /** |
||
387 | * Attention, column will be quoted by driver! |
||
388 | * |
||
389 | * @param string|null $column When column is null DISTINCT(PK) will be generated. |
||
390 | * |
||
391 | * @return int |
||
392 | */ |
||
393 | public function count(string $column = null): int |
||
406 | |||
407 | /** |
||
408 | * Query used as basement for relation. |
||
409 | * |
||
410 | * @return SelectQuery |
||
411 | */ |
||
412 | public function initialQuery(): SelectQuery |
||
416 | |||
417 | /** |
||
418 | * Get compiled version of SelectQuery, attentionly only first level query access is allowed. |
||
419 | * |
||
420 | * @return SelectQuery |
||
421 | */ |
||
422 | public function compiledQuery(): SelectQuery |
||
426 | |||
427 | /** |
||
428 | * Load data tree from databases and linked loaders in a form of array. |
||
429 | * |
||
430 | * @param OutputNode $node When empty node will be created automatically by root relation |
||
431 | * loader. |
||
432 | * |
||
433 | * @return array |
||
434 | */ |
||
435 | public function fetchData(OutputNode $node = null): array |
||
445 | |||
446 | /** |
||
447 | * {@inheritdoc} |
||
448 | */ |
||
449 | public function hasPaginator(): bool |
||
453 | |||
454 | /** |
||
455 | * {@inheritdoc} |
||
456 | */ |
||
457 | public function setPaginator(PaginatorInterface $paginator) |
||
461 | |||
462 | /** |
||
463 | * {@inheritdoc} |
||
464 | */ |
||
465 | public function getPaginator(): PaginatorInterface |
||
469 | |||
470 | /** |
||
471 | * Bypassing call to primary select query. |
||
472 | * |
||
473 | * @param string $name |
||
474 | * @param $arguments |
||
475 | * |
||
476 | * @return $this|mixed |
||
477 | */ |
||
478 | public function __call(string $name, array $arguments) |
||
494 | |||
495 | /** |
||
496 | * Cloning with loader tree cloning. |
||
497 | * |
||
498 | * @attention at this moment binded query parameters would't be cloned! |
||
499 | */ |
||
500 | public function __clone() |
||
504 | |||
505 | /** |
||
506 | * Remove nested loaders and clean ORM link. |
||
507 | */ |
||
508 | public function __destruct() |
||
513 | |||
514 | /** |
||
515 | * @return \Interop\Container\ContainerInterface|null |
||
516 | */ |
||
517 | protected function iocContainer() |
||
526 | } |