1 | <?php |
||
13 | class QueryBuilderFlex extends QueryBuilderHandler |
||
14 | { |
||
15 | /** @var string The column name of the column dedicated to storing the name of the model */ |
||
16 | protected $modelNameColumn; |
||
17 | |||
18 | /** @var Model|string The FQN of the model object this QueryBuilder instance is for */ |
||
19 | protected $modelType = null; |
||
20 | |||
21 | /** @var int The amount of results per page with regards to result pagination */ |
||
22 | private $resultsPerPage; |
||
23 | |||
24 | // |
||
25 | // Factories |
||
26 | // |
||
27 | |||
28 | /** |
||
29 | * Create a bare QueryBuilder instance. |
||
30 | * |
||
31 | * @throws Exception |
||
32 | * |
||
33 | * @return static |
||
34 | */ |
||
35 | final public static function createBuilder() |
||
43 | |||
44 | /** |
||
45 | * Create a QueryBuilder instance for a specific table. |
||
46 | * |
||
47 | * @param string $tableName |
||
48 | * |
||
49 | * @throws Exception If there is no database connection configured. |
||
50 | * |
||
51 | * @return static |
||
52 | */ |
||
53 | final public static function createForTable(string $tableName) |
||
59 | |||
60 | /** |
||
61 | * Creeate a QueryBuilder instance to work with a Model. |
||
62 | * |
||
63 | * @param string $modelType The FQN for the model that |
||
64 | * |
||
65 | * @throws Exception If there is no database connection configured. |
||
66 | * |
||
67 | * @return static |
||
68 | */ |
||
69 | final public static function createForModel(string $modelType) |
||
76 | |||
77 | // |
||
78 | // Overridden QueryBuilder Functions |
||
79 | // |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | public function __construct(Connection $connection = null) |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | * |
||
94 | * @internal Use one of the QueryBuilderFlex get*() methods instead. |
||
95 | * |
||
96 | * @see self::getArray() |
||
97 | * @see self::getModels() |
||
98 | * @see self::getNames() |
||
99 | */ |
||
100 | public function get(): array |
||
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | public function limit($limit): IQueryBuilderHandler |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | protected function whereHandler($key, string $operator = null, $value = null, $joiner = 'AND'): IQueryBuilderHandler |
||
126 | |||
127 | // |
||
128 | // QueryBuilderFlex unique functions |
||
129 | // |
||
130 | |||
131 | /** |
||
132 | * Request that only non-deleted Models should be returned. |
||
133 | * |
||
134 | * @return static |
||
135 | */ |
||
136 | public function active(): QueryBuilderFlex |
||
165 | |||
166 | /** |
||
167 | * An alias for QueryBuilder::getModels(), with fast fetching on by default and no return of results. |
||
168 | * |
||
169 | * @param bool $fastFetch Whether to perform one query to load all the model data instead of fetching them one by |
||
170 | * one |
||
171 | * |
||
172 | * @throws \Pecee\Pixie\Exception |
||
173 | * |
||
174 | * @return void |
||
175 | */ |
||
176 | public function addToCache(bool $fastFetch = true): void |
||
180 | |||
181 | /** |
||
182 | * Get the amount of pages this query would have. |
||
183 | * |
||
184 | * @throws \Pecee\Pixie\Exception |
||
185 | * |
||
186 | * @return int |
||
187 | */ |
||
188 | public function countPages(): int |
||
192 | |||
193 | /** |
||
194 | * Request that a specific model is not returned. |
||
195 | * |
||
196 | * @param Model|int $model The ID or model you don't want to get |
||
197 | * |
||
198 | * @return static |
||
199 | */ |
||
200 | public function except($model): QueryBuilderFlex |
||
210 | |||
211 | /** |
||
212 | * Find the first matching model in the database or return an invalid model. |
||
213 | * |
||
214 | * @param mixed $value The value to search for |
||
215 | * @param string $columnName The column name we'll be checking |
||
216 | * |
||
217 | * @throws \Pecee\Pixie\Exception |
||
218 | * |
||
219 | * @return Model |
||
220 | */ |
||
221 | public function findModel($value, string $columnName = 'id'): Model |
||
234 | |||
235 | /** |
||
236 | * Only show results from a specific page. |
||
237 | * |
||
238 | * This method will automatically take care of the calculations for a correct OFFSET. |
||
239 | * |
||
240 | * @param int|null $page The page number (or null to show all pages - counting starts from 0) |
||
241 | * |
||
242 | * @throws \Pecee\Pixie\Exception |
||
243 | * |
||
244 | * @return static |
||
245 | */ |
||
246 | public function fromPage(int $page = null): QueryBuilderFlex |
||
261 | |||
262 | /** |
||
263 | * Get the results of query as an array. |
||
264 | * |
||
265 | * @param array|string $columns |
||
266 | * |
||
267 | * @throws \Pecee\Pixie\Exception |
||
268 | * |
||
269 | * @return array |
||
270 | */ |
||
271 | public function getArray($columns): array |
||
284 | |||
285 | /** |
||
286 | * Perform the query and get the results as Models. |
||
287 | * |
||
288 | * @param bool $fastFetch Whether to perform one query to load all the model data instead of fetching them one by |
||
289 | * one (ignores cache) |
||
290 | * |
||
291 | * @throws \Pecee\Pixie\Exception |
||
292 | * |
||
293 | * @return Model[] |
||
294 | */ |
||
295 | public function getModels(bool $fastFetch = true): array |
||
324 | |||
325 | /** |
||
326 | * Perform the query and get back the results in an array of names. |
||
327 | * |
||
328 | * @throws \Pecee\Pixie\Exception |
||
329 | * @throws UnexpectedValueException When no name column has been specified |
||
330 | * |
||
331 | * @return string[] An array of the type $id => $name |
||
332 | */ |
||
333 | public function getNames(): array |
||
351 | |||
352 | /** |
||
353 | * Set the model this QueryBuilder will be working this. |
||
354 | * |
||
355 | * This information is used for automatically retrieving table names, eager columns, and lazy columns for these |
||
356 | * models. |
||
357 | * |
||
358 | * @param string $modelType The FQN of the model this QueryBuilder will be working with |
||
359 | * |
||
360 | * @return $this |
||
361 | */ |
||
362 | public function setModelType(string $modelType = null): QueryBuilderFlex |
||
368 | |||
369 | /** |
||
370 | * Set the column that'll be used as the human-friendly name of the model. |
||
371 | * |
||
372 | * @param string $columnName |
||
373 | * |
||
374 | * @return static |
||
375 | */ |
||
376 | public function setNameColumn(string $columnName): QueryBuilderFlex |
||
386 | |||
387 | /** |
||
388 | * Make sure that Models invisible to a player are not returned. |
||
389 | * |
||
390 | * Note that this method does not take PermissionModel::canBeSeenBy() into |
||
391 | * consideration for performance purposes, so you will have to override this |
||
392 | * in your query builder if necessary. |
||
393 | * |
||
394 | * @param Player $player The player in question |
||
395 | * @param bool $showDeleted Use false to hide deleted models even from admins |
||
396 | * |
||
397 | * @return static |
||
398 | */ |
||
399 | public function visibleTo(Player $player, bool $showDeleted = false): QueryBuilderFlex |
||
420 | } |
||
421 |
If you suppress an error, we recommend checking for the error condition explicitly: