QueryBuilder
class QueryBuilder implements Countable
This class can be used to search for models with specific characteristics in the database.
Note that most methods of this class return itself, so that you can easily add a number of different filters.
return Team::getQueryBuilder()
->active()
->where('name')->startsWith('a')
->sortBy('name')->reverse()
->getModels();
Methods
Create a new QueryBuilder
Select a column
Request that a column equals a string (case-insensitive)
Request that a column doesNOT equals a string (case-insensitive)
Request that a column is greater than a quantity
Request that a column is less than a quantity
Request that a column equals one of some strings
Request that a column value starts with a string (case-insensitive)
Return the results sorted by the value of a column
Reverse the order
Specify the number of results per page
Only show results from a specific page
Request that only "active" Models should be returned
Perform the query and get back the results in an array of names
Perform the query and get back the results in a list of arrays
An alias for QueryBuilder::getModels(), with fast fetching on by default and no return of results
Perform the query and get the results as Models
Count the results
Count the number of pages that all the models could be separated into
Find if there is any result
Get the amount of results that are returned per page
Details
at line line 139
__construct(string $type, array $options = array())
Create a new QueryBuilder
A new query builder should be created on a static getQueryBuilder() method on each model. The options array can contain the following properties:
columns
: An associative array - the key of each entry is the column name that will be used by other methods, while the value is is the column name that is used in the database structureactiveStatuses
: If the model has a status column, this should be a list of values that make the entry be considered "active"name
: The name of the column which represents the name of the object
at line line 160
QueryBuilder
where(string $column)
Select a column
$queryBuilder->where('username')->equals('administrator');
at line line 177
QueryBuilder
equals(string $string)
Request that a column equals a string (case-insensitive)
at line line 190
QueryBuilder
notEquals(string $string)
Request that a column doesNOT equals a string (case-insensitive)
at line line 203
QueryBuilder
greaterThan(string $quantity)
Request that a column is greater than a quantity
at line line 216
QueryBuilder
lessThan(string $quantity)
Request that a column is less than a quantity
at line line 232
QueryBuilder
isBefore(string|TimeDate $time, bool $inclusive = false, bool $reverse = false)
Request that a timestamp is before the specified time
at line line 246
QueryBuilder
isAfter(string|TimeDate $time, bool $inclusive = false, bool $reverse = false)
Request that a timestamp is after the specified time
at line line 268
QueryBuilder
is(int|Model|null $number)
Request that a column equals a number
at line line 290
QueryBuilder
isOneOf(string[] $strings)
Request that a column equals one of some strings
at line line 309
QueryBuilder
startsWith(string $string)
Request that a column value starts with a string (case-insensitive)
at line line 322
QueryBuilder
except(Model|int $model)
Request that a specific model is not returned
at line line 340
QueryBuilder
sortBy(string $column)
Return the results sorted by the value of a column
at line line 358
QueryBuilder
reverse()
Reverse the order
Note: This only works if you have specified a column in the sortBy() method
at line line 371
QueryBuilder
limit(int $count)
Specify the number of results per page
at line line 384
QueryBuilder
fromPage(int|null $page)
Only show results from a specific page
at line line 399
QueryBuilder
endAt(int|Model $model, bool $inclusive = false, bool $reverse = false)
End with a specific result
at line line 412
QueryBuilder
startAt(int|Model $model, bool $inclusive = false, bool $reverse = false)
Start with a specific result
at line line 445
QueryBuilder
active()
Request that only "active" Models should be returned
at line line 467
QueryBuilder
visibleTo(Player $player, bool $showDeleted = false)
Make sure that Models invisible to a player are not returned
Note that this method does not take PermissionModel::canBeSeenBy() into consideration for performance purposes, so you will have to override this in your query builder if necessary.
at line line 491
string[]
getNames()
Perform the query and get back the results in an array of names
at line line 509
array[]
getArray(string|string[] $columns)
Perform the query and get back the results in a list of arrays
at line line 529
void
addToCache(bool $fastFetch = true)
An alias for QueryBuilder::getModels(), with fast fetching on by default and no return of results
at line line 543
array
getModels(bool $fastFetch = false)
Perform the query and get the results as Models
at line line 564
int
count()
Count the results
at line line 581
countPages()
Count the number of pages that all the models could be separated into
at line line 591
bool
any()
Find if there is any result
at line line 605
int
getResultsPerPage()
Get the amount of results that are returned per page