1 | <?php |
||
13 | class QueryBuilderFlex extends QueryBuilderHandler |
||
14 | { |
||
15 | /** @var array An array of values that'll be injected into returned database results */ |
||
16 | protected $injectedValues = []; |
||
17 | |||
18 | /** @var string The column name of the column dedicated to storing the name of the model */ |
||
19 | protected $modelNameColumn; |
||
20 | |||
21 | /** @var Model|string The FQN of the model object this QueryBuilder instance is for */ |
||
22 | protected $modelType = null; |
||
23 | |||
24 | /** @var int The amount of results per page with regards to result pagination */ |
||
25 | private $resultsPerPage; |
||
26 | |||
27 | // |
||
28 | // Factories |
||
29 | // |
||
30 | |||
31 | /** |
||
32 | * Create a bare QueryBuilder instance. |
||
33 | * |
||
34 | * @throws Exception |
||
35 | * |
||
36 | * @return static |
||
37 | */ |
||
38 | final public static function createBuilder() |
||
46 | |||
47 | /** |
||
48 | * Create a QueryBuilder instance for a specific table. |
||
49 | * |
||
50 | * @param string $tableName |
||
51 | * |
||
52 | * @throws Exception If there is no database connection configured. |
||
53 | * |
||
54 | * @return static |
||
55 | */ |
||
56 | final public static function createForTable(string $tableName) |
||
62 | |||
63 | /** |
||
64 | * Creeate a QueryBuilder instance to work with a Model. |
||
65 | * |
||
66 | * @param string $modelType The FQN for the model that |
||
67 | * |
||
68 | * @throws Exception If there is no database connection configured. |
||
69 | * |
||
70 | * @return static |
||
71 | */ |
||
72 | final public static function createForModel(string $modelType) |
||
79 | |||
80 | // |
||
81 | // Overridden QueryBuilder Functions |
||
82 | // |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | public function __construct(Connection $connection = null) |
||
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | * |
||
97 | * @internal Use one of the QueryBuilderFlex get*() methods instead. |
||
98 | * |
||
99 | * @see self::getArray() |
||
100 | * @see self::getModels() |
||
101 | * @see self::getNames() |
||
102 | */ |
||
103 | public function get(): array |
||
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | public function limit($limit): IQueryBuilderHandler |
||
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | protected function whereHandler($key, string $operator = null, $value = null, $joiner = 'AND'): IQueryBuilderHandler |
||
141 | |||
142 | // |
||
143 | // QueryBuilderFlex unique functions |
||
144 | // |
||
145 | |||
146 | /** |
||
147 | * Request that only non-deleted Models should be returned. |
||
148 | * |
||
149 | * @return static |
||
150 | */ |
||
151 | public function active(): QueryBuilderFlex |
||
180 | |||
181 | /** |
||
182 | * An alias for QueryBuilder::getModels(), with fast fetching on by default and no return of results. |
||
183 | * |
||
184 | * @param bool $fastFetch Whether to perform one query to load all the model data instead of fetching them one by |
||
185 | * one |
||
186 | * |
||
187 | * @throws \Pecee\Pixie\Exception |
||
188 | * |
||
189 | * @return void |
||
190 | */ |
||
191 | public function addToCache(bool $fastFetch = true): void |
||
195 | |||
196 | /** |
||
197 | * Get the amount of pages this query would have. |
||
198 | * |
||
199 | * @throws \Pecee\Pixie\Exception |
||
200 | * |
||
201 | * @return int |
||
202 | */ |
||
203 | public function countPages(): int |
||
207 | |||
208 | /** |
||
209 | * Request that a specific model is not returned. |
||
210 | * |
||
211 | * @param Model|int $model The ID or model you don't want to get |
||
212 | * |
||
213 | * @return static |
||
214 | */ |
||
215 | public function except($model): QueryBuilderFlex |
||
225 | |||
226 | /** |
||
227 | * Find the first matching model in the database or return an invalid model. |
||
228 | * |
||
229 | * @param mixed $value The value to search for |
||
230 | * @param string $columnName The column name we'll be checking |
||
231 | * |
||
232 | * @throws \Pecee\Pixie\Exception |
||
233 | * |
||
234 | * @return Model |
||
235 | */ |
||
236 | public function findModel($value, string $columnName = 'id'): Model |
||
249 | |||
250 | /** |
||
251 | * Only show results from a specific page. |
||
252 | * |
||
253 | * This method will automatically take care of the calculations for a correct OFFSET. |
||
254 | * |
||
255 | * @param int|null $page The page number (or null to show all pages - counting starts from 0) |
||
256 | * |
||
257 | * @throws \Pecee\Pixie\Exception |
||
258 | * |
||
259 | * @return static |
||
260 | */ |
||
261 | public function fromPage(int $page = null): QueryBuilderFlex |
||
276 | |||
277 | /** |
||
278 | * Get the results of query as an array. |
||
279 | * |
||
280 | * @param array|string $columns |
||
281 | * |
||
282 | * @throws \Pecee\Pixie\Exception |
||
283 | * |
||
284 | * @return array |
||
285 | */ |
||
286 | public function getArray($columns): array |
||
292 | |||
293 | /** |
||
294 | * Perform the query and get the results as Models. |
||
295 | * |
||
296 | * @param bool $fastFetch Whether to perform one query to load all the model data instead of fetching them one by |
||
297 | * one (ignores cache) |
||
298 | * |
||
299 | * @throws \Pecee\Pixie\Exception |
||
300 | * |
||
301 | * @return Model[] |
||
302 | */ |
||
303 | public function getModels(bool $fastFetch = true): array |
||
326 | |||
327 | /** |
||
328 | * Perform the query and get back the results in an array of names. |
||
329 | * |
||
330 | * @throws \Pecee\Pixie\Exception |
||
331 | * @throws UnexpectedValueException When no name column has been specified |
||
332 | * |
||
333 | * @return string[] An array of the type $id => $name |
||
334 | */ |
||
335 | public function getNames(): array |
||
347 | |||
348 | /** |
||
349 | * Inject variables into the returned database results. |
||
350 | * |
||
351 | * These values will be merged in with values returned from database results. Database results will override any |
||
352 | * injected values. |
||
353 | * |
||
354 | * @param array $injection |
||
355 | * |
||
356 | * @return QueryBuilderFlex |
||
357 | */ |
||
358 | public function injectResultValues(array $injection): QueryBuilderFlex |
||
364 | |||
365 | /** |
||
366 | * Set the model this QueryBuilder will be working this. |
||
367 | * |
||
368 | * This information is used for automatically retrieving table names, eager columns, and lazy columns for these |
||
369 | * models. |
||
370 | * |
||
371 | * @param string $modelType The FQN of the model this QueryBuilder will be working with |
||
372 | * |
||
373 | * @return $this |
||
374 | */ |
||
375 | public function setModelType(string $modelType = null): QueryBuilderFlex |
||
381 | |||
382 | /** |
||
383 | * Set the column that'll be used as the human-friendly name of the model. |
||
384 | * |
||
385 | * @param string $columnName |
||
386 | * |
||
387 | * @return static |
||
388 | */ |
||
389 | public function setNameColumn(string $columnName): QueryBuilderFlex |
||
399 | |||
400 | /** |
||
401 | * Make sure that Models invisible to a player are not returned. |
||
402 | * |
||
403 | * Note that this method does not take PermissionModel::canBeSeenBy() into |
||
404 | * consideration for performance purposes, so you will have to override this |
||
405 | * in your query builder if necessary. |
||
406 | * |
||
407 | * @param Player $player The player in question |
||
408 | * @param bool $showDeleted Use false to hide deleted models even from admins |
||
409 | * |
||
410 | * @return static |
||
411 | */ |
||
412 | public function visibleTo(Player $player, bool $showDeleted = false): QueryBuilderFlex |
||
433 | } |
||
434 |
If you suppress an error, we recommend checking for the error condition explicitly: