1 | <?php |
||
26 | class RecordSelector extends Component implements \IteratorAggregate, \Countable, PaginatorAwareInterface |
||
27 | { |
||
28 | use SaturateTrait; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $class; |
||
34 | |||
35 | /** |
||
36 | * @invisible |
||
37 | * @var ORMInterface |
||
38 | */ |
||
39 | private $orm; |
||
40 | |||
41 | /** |
||
42 | * @var RootLoader |
||
43 | */ |
||
44 | private $loader; |
||
45 | |||
46 | /** |
||
47 | * @param string $class |
||
48 | * @param ORMInterface $orm |
||
49 | */ |
||
50 | public function __construct(string $class, ORMInterface $orm) |
||
61 | |||
62 | /** |
||
63 | * Get associated class. |
||
64 | * |
||
65 | * @return string |
||
66 | */ |
||
67 | public function getClass(): string |
||
71 | |||
72 | /** |
||
73 | * Request primary selector loader to pre-load relation name. Any type of loader can be used |
||
74 | * for |
||
75 | * data preloading. ORM loaders by default will select the most efficient way to load related |
||
76 | * data which might include additional select query or left join. Loaded data will |
||
77 | * automatically pre-populate record relations. You can specify nested relations using "." |
||
78 | * separator. |
||
79 | * |
||
80 | * Examples: |
||
81 | * |
||
82 | * //Select users and load their comments (will cast 2 queries, HAS_MANY comments) |
||
83 | * User::find()->with('comments'); |
||
84 | * |
||
85 | * //You can load chain of relations - select user and load their comments and post related to |
||
86 | * //comment |
||
87 | * User::find()->with('comments.post'); |
||
88 | * |
||
89 | * //We can also specify custom where conditions on data loading, let's load only public |
||
90 | * comments. User::find()->load('comments', [ |
||
91 | * 'where' => ['{@}.status' => 'public'] |
||
92 | * ]); |
||
93 | * |
||
94 | * Please note using "{@}" column name, this placeholder is required to prevent collisions and |
||
95 | * it will be automatically replaced with valid table alias of pre-loaded comments table. |
||
96 | * |
||
97 | * //In case where your loaded relation is MANY_TO_MANY you can also specify pivot table |
||
98 | * conditions, |
||
99 | * //let's pre-load all approved user tags, we can use same placeholder for pivot table alias |
||
100 | * User::find()->load('tags', [ |
||
101 | * 'wherePivot' => ['{@}.approved' => true] |
||
102 | * ]); |
||
103 | * |
||
104 | * //In most of cases you don't need to worry about how data was loaded, using external query |
||
105 | * or |
||
106 | * //left join, however if you want to change such behaviour you can force load method to |
||
107 | * INLOAD |
||
108 | * User::find()->load('tags', [ |
||
109 | * 'method' => Loader::INLOAD, |
||
110 | * 'wherePivot' => ['{@}.approved' => true] |
||
111 | * ]); |
||
112 | * |
||
113 | * Attention, you will not be able to correctly paginate in this case and only ORM loaders |
||
114 | * support different loading types. |
||
115 | * |
||
116 | * You can specify multiple loaders using array as first argument. |
||
117 | * |
||
118 | * Example: |
||
119 | * User::find()->load(['posts', 'comments', 'profile']); |
||
120 | * |
||
121 | * @see with() |
||
122 | * |
||
123 | * @param string|array $relation |
||
124 | * @param array $options |
||
125 | * |
||
126 | * @return $this|RecordSelector |
||
127 | */ |
||
128 | public function load($relation, array $options = []): self |
||
149 | |||
150 | /** |
||
151 | * With method is very similar to load() one, except it will always include related data to |
||
152 | * parent query using INNER JOIN, this method can be applied only to ORM loaders and relations |
||
153 | * using same database as parent record. |
||
154 | * |
||
155 | * Method generally used to filter data based on some relation condition. |
||
156 | * Attention, with() method WILL NOT load relation data, it will only make it accessible in |
||
157 | * query. |
||
158 | * |
||
159 | * By default joined tables will be available in query based on relation name, you can change |
||
160 | * joined table alias using relation option "alias". |
||
161 | * |
||
162 | * Do not forget to set DISTINCT flag while including HAS_MANY and MANY_TO_MANY relations. In |
||
163 | * other scenario you will not able to paginate data well. |
||
164 | * |
||
165 | * Examples: |
||
166 | * |
||
167 | * //Find all users who have comments comments |
||
168 | * User::find()->with('comments'); |
||
169 | * |
||
170 | * //Find all users who have approved comments (we can use comments table alias in where |
||
171 | * statement). |
||
172 | * User::find()->with('comments')->where('comments.approved', true); |
||
173 | * |
||
174 | * //Find all users who have posts which have approved comments |
||
175 | * User::find()->with('posts.comments')->where('posts_comments.approved', true); |
||
176 | * |
||
177 | * //Custom join alias for post comments relation |
||
178 | * $user->with('posts.comments', [ |
||
179 | * 'alias' => 'comments' |
||
180 | * ])->where('comments.approved', true); |
||
181 | * |
||
182 | * //If you joining MANY_TO_MANY relation you will be able to use pivot table used as relation |
||
183 | * name |
||
184 | * //plus "_pivot" postfix. Let's load all users with approved tags. |
||
185 | * $user->with('tags')->where('tags_pivot.approved', true); |
||
186 | * |
||
187 | * //You can also use custom alias for pivot table as well |
||
188 | * User::find()->with('tags', [ |
||
189 | * 'pivotAlias' => 'tags_connection' |
||
190 | * ]) |
||
191 | * ->where('tags_connection.approved', false); |
||
192 | * |
||
193 | * You can safely combine with() and load() methods. |
||
194 | * |
||
195 | * //Load all users with approved comments and pre-load all their comments |
||
196 | * User::find()->with('comments')->where('comments.approved', true) |
||
197 | * ->load('comments'); |
||
198 | * |
||
199 | * //You can also use custom conditions in this case, let's find all users with approved |
||
200 | * comments |
||
201 | * //and pre-load such approved comments |
||
202 | * User::find()->with('comments')->where('comments.approved', true) |
||
203 | * ->load('comments', [ |
||
204 | * 'where' => ['{@}.approved' => true] |
||
205 | * ]); |
||
206 | * |
||
207 | * //As you might notice previous construction will create 2 queries, however we can simplify |
||
208 | * //this construction to use already joined table as source of data for relation via "using" |
||
209 | * //keyword |
||
210 | * User::find()->with('comments') |
||
211 | * ->where('comments.approved', true) |
||
212 | * ->load('comments', ['using' => 'comments']); |
||
213 | * |
||
214 | * //You will get only one query with INNER JOIN, to better understand this example let's use |
||
215 | * //custom alias for comments in with() method. |
||
216 | * User::find()->with('comments', ['alias' => 'commentsR']) |
||
217 | * ->where('commentsR.approved', true) |
||
218 | * ->load('comments', ['using' => 'commentsR']); |
||
219 | * |
||
220 | * @see load() |
||
221 | * |
||
222 | * @param string|array $relation |
||
223 | * @param array $options |
||
224 | * |
||
225 | * @return $this|RecordSelector |
||
226 | */ |
||
227 | public function with($relation, array $options = []): self |
||
248 | |||
249 | /** |
||
250 | * Shortcut to where method to set AND condition for parent record primary key. |
||
251 | * |
||
252 | * @param string|int $id |
||
253 | * |
||
254 | * @return RecordSelector |
||
255 | * |
||
256 | * @throws SelectorException |
||
257 | */ |
||
258 | public function wherePK($id): self |
||
268 | |||
269 | /** |
||
270 | * Find one entity or return null. |
||
271 | * |
||
272 | * @param array|null $query |
||
273 | * |
||
274 | * @return EntityInterface|null |
||
275 | */ |
||
276 | public function findOne(array $query = null) |
||
286 | |||
287 | /** |
||
288 | * Get RecordIterator (entity iterator) for a requested data. Provide cache key and lifetime in |
||
289 | * order to cache request data. |
||
290 | * |
||
291 | * @param string $cacheKey |
||
292 | * @param int|\DateInterval $ttl |
||
293 | * @param CacheItemPoolInterface|null $pool |
||
294 | * |
||
295 | * @return RecordIterator |
||
296 | */ |
||
297 | public function getIterator( |
||
324 | |||
325 | /** |
||
326 | * @param string|null $column When column is null DISTINCT(PK) will be generated. |
||
327 | * |
||
328 | * @return int |
||
329 | */ |
||
330 | public function count(string $column = null): int |
||
343 | |||
344 | /** |
||
345 | * Get compiled version of SelectQuery, attentionly only first level query access is allowed. |
||
346 | * |
||
347 | * @return SelectQuery |
||
348 | */ |
||
349 | public function compileQuery(): SelectQuery |
||
353 | |||
354 | /** |
||
355 | * Load data tree from databases and linked loaders in a form of array. |
||
356 | * |
||
357 | * @return array |
||
358 | */ |
||
359 | public function fetchData(): array |
||
371 | |||
372 | /** |
||
373 | * {@inheritdoc} |
||
374 | */ |
||
375 | public function hasPaginator(): bool |
||
379 | |||
380 | /** |
||
381 | * {@inheritdoc} |
||
382 | */ |
||
383 | public function setPaginator(PaginatorInterface $paginator) |
||
387 | |||
388 | /** |
||
389 | * {@inheritdoc} |
||
390 | */ |
||
391 | public function getPaginator(): PaginatorInterface |
||
395 | |||
396 | /** |
||
397 | * Bypassing call to primary select query. |
||
398 | * |
||
399 | * @param string $name |
||
400 | * @param $arguments |
||
401 | * |
||
402 | * @return $this|mixed |
||
403 | */ |
||
404 | public function __call(string $name, array $arguments) |
||
420 | |||
421 | /** |
||
422 | * Cloning with loader tree cloning. |
||
423 | * |
||
424 | * @attention at this moment binded query parameters would't be cloned! |
||
425 | */ |
||
426 | public function __clone() |
||
430 | |||
431 | /** |
||
432 | * Remove nested loaders and clean ORM link. |
||
433 | */ |
||
434 | public function __destruct() |
||
439 | |||
440 | /** |
||
441 | * @return \Interop\Container\ContainerInterface|null |
||
442 | */ |
||
443 | protected function iocContainer() |
||
452 | } |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: