QueryBuilder
in package
Table of Contents
Constants
- DEFAULT_SELECT = '*'
Properties
- $collections : array<string|int, mixed>
- $driver : null|DriverBase
- $fields : array<string|int, mixed>
- $filters : array<string|int, mixed>
- $formatting : array<string|int, mixed>
- $groups : array<string|int, mixed>
- $joins : array<string|int, mixed>
- $limit : null|int
- $logger : LoggerInterface
- $offset : null|int
- $sorts : array<string|int, mixed>
Methods
- __construct() : mixed
- add() : int
- Alias for insert method.
- addMultiple() : int
- Alias for insert multiple method.
- create() : QueryBuilder
- delete() : int
- Execute a query to delete record(s) and returns the number of affected rows.
- destroy() : int
- Alias for delete method.
- fetch() : null|array<string|int, mixed>
- Simple query mechanism to find record(s) by a field and its value.
- filter() : QueryBuilder
- Add a filter to be used in the query to be executed.
- filterList() : QueryBuilder
- Add a list of filters to be used in the query to be executed.
- filterOn() : QueryBuilder
- Create a filter to be used in the query to be executed.
- filterOnFields() : QueryBuilder
- Create a filter to be used in the query to be executed.
- formatAs() : QueryBuilder
- Format the query result.
- get() : null|array<string|int, mixed>
- Alias for select method.
- groupOn() : QueryBuilder
- Group the query result.
- insert() : int
- Execute a query inserting a record and returns the corresponding primary id.
- insertMultiple() : int
- Execute a query inserting multiple records in a single statement.
- joinOn() : QueryBuilder
- Join collections to the query.
- limit() : QueryBuilder
- Limit the query result.
- modify() : int
- Alias for update method.
- offset() : QueryBuilder
- Offset the query result.
- orderOn() : QueryBuilder
- Order the query result.
- select() : null|array<string|int, mixed>
- Execute a query finding record(s) and returns the result.
- sortOn() : QueryBuilder
- Alias for order method.
- update() : int
- Execute a query updating record(s) and return the number of affected rows.
Constants
DEFAULT_SELECT
private
mixed
DEFAULT_SELECT
= '*'
Properties
$collections
private
array<string|int, mixed>
$collections
= []
$driver
private
null|DriverBase
$driver
= null
$fields
private
array<string|int, mixed>
$fields
= []
$filters
private
array<string|int, mixed>
$filters
= []
$formatting
private
array<string|int, mixed>
$formatting
= []
$groups
private
array<string|int, mixed>
$groups
= []
$joins
private
array<string|int, mixed>
$joins
= []
$limit
private
null|int
$limit
= null
$logger
private
LoggerInterface
$logger
$offset
private
null|int
$offset
= null
$sorts
private
array<string|int, mixed>
$sorts
= []
Methods
__construct()
public
__construct(null|DriverBase $driver, array<string|int, mixed> $collections[, LoggerInterface $logger = new NullLogger() ]) : mixed
Parameters
- $driver : null|DriverBase
- $collections : array<string|int, mixed>
- $logger : LoggerInterface = new NullLogger()
add()
Alias for insert method.
public
add(array<string|int, mixed> $fieldsWithValues) : int
Parameters
- $fieldsWithValues : array<string|int, mixed>
Return values
intaddMultiple()
Alias for insert multiple method.
public
addMultiple(array<string|int, mixed> $fieldsWithValues) : int
Parameters
- $fieldsWithValues : array<string|int, mixed>
Return values
intcreate()
public
static create([null|DriverBase $driver = null ][, array<string|int, mixed> $collections = [] ][, LoggerInterface $logger = new NullLogger() ]) : QueryBuilder
Parameters
- $driver : null|DriverBase = null
- $collections : array<string|int, mixed> = []
- $logger : LoggerInterface = new NullLogger()
Return values
QueryBuilderdelete()
Execute a query to delete record(s) and returns the number of affected rows.
public
delete() : int
- Example use: $database->query('CollectionName')->filterOn( ... )->delete()
Return values
intdestroy()
Alias for delete method.
public
destroy() : int
Return values
intfetch()
Simple query mechanism to find record(s) by a field and its value.
public
fetch(string $field, mixed $value[, Operator $operator = Operator::EQ ]) : null|array<string|int, mixed>
- Example use: $database->query('CollectionName')->fetch('Name', 'John')
- Example use: $database->query('CollectionName')->fetch('Age', [25, 55], Operator::IN)
Parameters
- $field : string
- $value : mixed
- $operator : Operator = Operator::EQ
Return values
null|array<string|int, mixed>filter()
Add a filter to be used in the query to be executed.
public
filter(array<string|int, mixed> $filter[, LogicalOperator $logicalOperator = LogicalOperator::AND ]) : QueryBuilder
Parameters
- $filter : array<string|int, mixed>
-
['Name', Operator::EQ, 'John', LogicalOperator::AND]
- Example use: $database->query('CollectionName')->filter(['Name', Operator::EQ, 'John', LogicalOperator::AND])
- $logicalOperator : LogicalOperator = LogicalOperator::AND
Return values
QueryBuilderfilterList()
Add a list of filters to be used in the query to be executed.
public
filterList(array<string|int, mixed> $filters[, LogicalOperator $logicalOperator = LogicalOperator::AND ]) : QueryBuilder
Parameters
- $filters : array<string|int, mixed>
-
[['Name', Operator::EQ, 'John', LogicalOperator::AND], [ ... ], [ ... ]]
- Example use: $database->query('CollectionName')->filterList([['Name', Operator::EQ, 'John', LogicalOperator::AND]])
- $logicalOperator : LogicalOperator = LogicalOperator::AND
Return values
QueryBuilderfilterOn()
Create a filter to be used in the query to be executed.
public
filterOn(string $field, mixed $value[, Operator $operator = Operator::EQ ][, LogicalOperator $logicalOperator = LogicalOperator::AND ]) : QueryBuilder
Parameters
- $field : string
-
'Name'
- $value : mixed
-
'John'
- Example use: $database->query('CollectionName')->filterOn('Name', 'John')
- $operator : Operator = Operator::EQ
- $logicalOperator : LogicalOperator = LogicalOperator::AND
Return values
QueryBuilderfilterOnFields()
Create a filter to be used in the query to be executed.
public
filterOnFields(array<string|int, mixed> $fieldsWithValues[, Operator $operator = Operator::EQ ][, LogicalOperator $logicalOperator = LogicalOperator::AND ]) : QueryBuilder
Parameters
- $fieldsWithValues : array<string|int, mixed>
-
['Name' => 'John', 'Lastname' => 'Doe', 'UserID' => 25, ..., ...]
- Example use: $database->query('CollectionName')->filterList(['Name' => 'John', 'Deleted' => 0 ])
- $operator : Operator = Operator::EQ
- $logicalOperator : LogicalOperator = LogicalOperator::AND
Return values
QueryBuilderformatAs()
Format the query result.
public
formatAs(Format $format[, mixed $args = null ]) : QueryBuilder
- Example use: $database->query('CollectionName')->formatAs(Format::STD_CLASS)
- Example use: $database->query('CollectionName')->formatAs(Format::CUSTOM_OBJECT, ClassName::class)
Parameters
- $format : Format
- $args : mixed = null
Return values
QueryBuilderget()
Alias for select method.
public
get(string ...$fields) : null|array<string|int, mixed>
Parameters
- $fields : string
Return values
null|array<string|int, mixed>groupOn()
Group the query result.
public
groupOn(string ...$fields) : QueryBuilder
- Example use: $database->query('CollectionName')->groupOn('Name')
- Example use: $database->query('CollectionName')->groupOn('Name', 'Age')
- Example use: $database->query('CollectionName')->groupOn('Name, Age')
Parameters
- $fields : string
Return values
QueryBuilderinsert()
Execute a query inserting a record and returns the corresponding primary id.
public
insert(array<string|int, mixed> $fieldsWithValues) : int
- Example use: $database->query('CollectionName')->insert(['Username' => 'John', 'Password' => '1234'])
Parameters
- $fieldsWithValues : array<string|int, mixed>
Return values
intinsertMultiple()
Execute a query inserting multiple records in a single statement.
public
insertMultiple(array<string|int, mixed> $fieldsWithValues) : int
Returns the primary id of the first inserted record.
- Example use: $database->query('CollectionName')->insert([['Username' => 'John', 'Password' => '1234']])
Parameters
- $fieldsWithValues : array<string|int, mixed>
Return values
intjoinOn()
Join collections to the query.
public
joinOn(string $currentCollectionWithField, Join $join[, string|null $onCollectionWithField = null ]) : QueryBuilder
- Example use: $database->query('CollectionName')->join('Collection1.Field', Join::INNER, 'Collection2.Field')
- Example use: $database->query('CollectionName')->join('Collection1.Field = Collection2.Field', Join::LEFT)
Parameters
- $currentCollectionWithField : string
- $join : Join
- $onCollectionWithField : string|null = null
Return values
QueryBuilderlimit()
Limit the query result.
public
limit(int $limit) : QueryBuilder
- Example use: $database->query('CollectionName')->limit(10)
- Example use: $database->query('CollectionName')->limit('10')
Parameters
- $limit : int
Return values
QueryBuildermodify()
Alias for update method.
public
modify(array<string|int, mixed> $fieldsWithValues) : int
Parameters
- $fieldsWithValues : array<string|int, mixed>
Return values
intoffset()
Offset the query result.
public
offset(int $offset) : QueryBuilder
- Example use: $database->query('CollectionName')->offset(10)
Parameters
- $offset : int
Return values
QueryBuilderorderOn()
Order the query result.
public
orderOn(array<string|int, mixed> $sortFields) : QueryBuilder
- Example use: $database->query('CollectionName')->sortOn(['Name' => Sorts::DESC])
- Example use: $database->query('CollectionName')->sortOn(['Name' => Sorts::ASC, 'Age' => Sorts::DESC])
Parameters
- $sortFields : array<string|int, mixed>
Return values
QueryBuilderselect()
Execute a query finding record(s) and returns the result.
public
select(string ...$fields) : null|array<string|int, mixed>
- Example use: $database->query('CollectionName')->select('*')
- Example use: $database->query('CollectionName')->select('Name', 'Age')
- Example use: $database->query('CollectionName')->select('Name, Age')
- Example use: $database->query('CollectionName')->select('Name as Username')
- Example use: $database->query('CollectionName')->select([ 'Name', 'Age', 'Username' ])
Parameters
- $fields : string
Return values
null|array<string|int, mixed>sortOn()
Alias for order method.
public
sortOn(array<string|int, mixed> $sortFields) : QueryBuilder
Parameters
- $sortFields : array<string|int, mixed>
Return values
QueryBuilderupdate()
Execute a query updating record(s) and return the number of affected rows.
public
update(array<string|int, mixed> $fieldsWithValues) : int
- Example use: $database->query('CollectionName')->update(['Active' => 1])
- Example use: $database->query('CollectionName')->filterOn( ... )->update(['Username' => 'Jane', 'Password' => 'password'])
Parameters
- $fieldsWithValues : array<string|int, mixed>