HumHub Documentation (unofficial)

QueryTrait

The BaseQuery trait represents the minimum method set of a database Query.

It is supposed to be used in a class that implements the [[QueryInterface]].

Tags
author

Qiang Xue qiang.xue@gmail.com

author

Carsten Brandt mail@cebe.cc

since
2.0

Table of Contents

Properties

$emulateExecution  : bool
$indexBy  : string|callable|null
$limit  : int|ExpressionInterface|null
$offset  : int|ExpressionInterface|null
$orderBy  : array<string|int, mixed>|null
$where  : string|array<string|int, mixed>|ExpressionInterface|null

Methods

addOrderBy()  : $this
Adds additional ORDER BY columns to the query.
andFilterWhere()  : $this
Adds an additional WHERE condition to the existing one but ignores [[isEmpty()|empty operands]].
andWhere()  : $this
Adds an additional WHERE condition to the existing one.
emulateExecution()  : $this
Sets whether to emulate query execution, preventing any interaction with data storage.
filterWhere()  : $this
Sets the WHERE part of the query but ignores [[isEmpty()|empty operands]].
indexBy()  : $this
Sets the [[indexBy]] property.
limit()  : $this
Sets the LIMIT part of the query.
offset()  : $this
Sets the OFFSET part of the query.
orderBy()  : $this
Sets the ORDER BY part of the query.
orFilterWhere()  : $this
Adds an additional WHERE condition to the existing one but ignores [[isEmpty()|empty operands]].
orWhere()  : $this
Adds an additional WHERE condition to the existing one.
where()  : $this
Sets the WHERE part of the query.
filterCondition()  : array<string|int, mixed>
Removes [[isEmpty()|empty operands]] from the given query condition.
isEmpty()  : bool
Returns a value indicating whether the give value is "empty".
normalizeOrderBy()  : array<string|int, mixed>
Normalizes format of ORDER BY data.

Properties

$emulateExecution

public bool $emulateExecution = false

whether to emulate the actual query execution, returning empty or false results.

Tags
see
emulateExecution()
since
2.0.11

$indexBy

public string|callable|null $indexBy

the name of the column by which the query results should be indexed by. This can also be a callable (e.g. anonymous function) that returns the index value based on the given row data. For more details, see [[indexBy()]]. This property is only used by [[QueryInterface::all()|all()]].

$limit

public int|ExpressionInterface|null $limit

maximum number of records to be returned. May be an instance of [[ExpressionInterface]]. If not set or less than 0, it means no limit.

$offset

public int|ExpressionInterface|null $offset

zero-based offset from where the records are to be returned. May be an instance of [[ExpressionInterface]]. If not set or less than 0, it means starting from the beginning.

$orderBy

public array<string|int, mixed>|null $orderBy

how to sort the query results. This is used to construct the ORDER BY clause in a SQL statement. The array keys are the columns to be sorted by, and the array values are the corresponding sort directions which can be either SORT_ASC or SORT_DESC. The array may also contain [[ExpressionInterface]] objects. If that is the case, the expressions will be converted into strings without any change.

$where

public string|array<string|int, mixed>|ExpressionInterface|null $where

query condition. This refers to the WHERE clause in a SQL statement. For example, ['age' => 31, 'team' => 1].

Tags
see
where()

for valid syntax on specifying this value.

Methods

addOrderBy()

Adds additional ORDER BY columns to the query.

public addOrderBy(string|array<string|int, mixed>|ExpressionInterface $columns) : $this
Parameters
$columns : string|array<string|int, mixed>|ExpressionInterface

the columns (and the directions) to be ordered by. Columns can be specified in either a string (e.g. "id ASC, name DESC") or an array (e.g. ['id' => SORT_ASC, 'name' => SORT_DESC]).

The method will automatically quote the column names unless a column contains some parenthesis (which means the column contains a DB expression).

Note that if your order-by is an expression containing commas, you should always use an array to represent the order-by information. Otherwise, the method will not be able to correctly determine the order-by columns.

Since version 2.0.7, an [[ExpressionInterface]] object can be passed to specify the ORDER BY part explicitly in plain SQL.

Tags
see
orderBy()
Return values
$this

the query object itself

andFilterWhere()

Adds an additional WHERE condition to the existing one but ignores [[isEmpty()|empty operands]].

public andFilterWhere(array<string|int, mixed> $condition) : $this

The new condition and the existing one will be joined using the 'AND' operator.

This method is similar to [[andWhere()]]. The main difference is that this method will remove [[isEmpty()|empty query operands]]. As a result, this method is best suited for building query conditions based on filter values entered by users.

Parameters
$condition : array<string|int, mixed>

the new WHERE condition. Please refer to [[where()]] on how to specify this parameter.

Tags
see
filterWhere()
see
orFilterWhere()
Return values
$this

the query object itself

andWhere()

Adds an additional WHERE condition to the existing one.

public andWhere(string|array<string|int, mixed>|ExpressionInterface $condition) : $this

The new condition and the existing one will be joined using the 'AND' operator.

Parameters
$condition : string|array<string|int, mixed>|ExpressionInterface

the new WHERE condition. Please refer to [[where()]] on how to specify this parameter.

Tags
see
where()
see
orWhere()
Return values
$this

the query object itself

emulateExecution()

Sets whether to emulate query execution, preventing any interaction with data storage.

public emulateExecution([bool $value = true ]) : $this

After this mode is enabled, methods, returning query results like [[QueryInterface::one()]], [[QueryInterface::all()]], [[QueryInterface::exists()]] and so on, will return empty or false values. You should use this method in case your program logic indicates query should not return any results, like in case you set false where condition like 0=1.

Parameters
$value : bool = true

whether to prevent query execution.

Tags
since
2.0.11
Return values
$this

the query object itself.

filterWhere()

Sets the WHERE part of the query but ignores [[isEmpty()|empty operands]].

public filterWhere(array<string|int, mixed> $condition) : $this

This method is similar to [[where()]]. The main difference is that this method will remove [[isEmpty()|empty query operands]]. As a result, this method is best suited for building query conditions based on filter values entered by users.

The following code shows the difference between this method and [[where()]]:

// WHERE `age`=:age
$query->filterWhere(['name' => null, 'age' => 20]);
// WHERE `age`=:age
$query->where(['age' => 20]);
// WHERE `name` IS NULL AND `age`=:age
$query->where(['name' => null, 'age' => 20]);

Note that unlike [[where()]], you cannot pass binding parameters to this method.

Parameters
$condition : array<string|int, mixed>

the conditions that should be put in the WHERE part. See [[where()]] on how to specify this parameter.

Tags
see
where()
see
andFilterWhere()
see
orFilterWhere()
Return values
$this

the query object itself

indexBy()

Sets the [[indexBy]] property.

public indexBy(string|callable $column) : $this
Parameters
$column : string|callable

the name of the column by which the query results should be indexed by. This can also be a callable (e.g. anonymous function) that returns the index value based on the given row data. The signature of the callable should be:

function ($row)
{
    // return the index value corresponding to $row
}
Return values
$this

the query object itself

limit()

Sets the LIMIT part of the query.

public limit(int|ExpressionInterface|null $limit) : $this
Parameters
$limit : int|ExpressionInterface|null

the limit. Use null or negative value to disable limit.

Return values
$this

the query object itself

offset()

Sets the OFFSET part of the query.

public offset(int|ExpressionInterface|null $offset) : $this
Parameters
$offset : int|ExpressionInterface|null

the offset. Use null or negative value to disable offset.

Return values
$this

the query object itself

orderBy()

Sets the ORDER BY part of the query.

public orderBy(string|array<string|int, mixed>|ExpressionInterface|null $columns) : $this
Parameters
$columns : string|array<string|int, mixed>|ExpressionInterface|null

the columns (and the directions) to be ordered by. Columns can be specified in either a string (e.g. "id ASC, name DESC") or an array (e.g. ['id' => SORT_ASC, 'name' => SORT_DESC]).

The method will automatically quote the column names unless a column contains some parenthesis (which means the column contains a DB expression).

Note that if your order-by is an expression containing commas, you should always use an array to represent the order-by information. Otherwise, the method will not be able to correctly determine the order-by columns.

Since version 2.0.7, an [[ExpressionInterface]] object can be passed to specify the ORDER BY part explicitly in plain SQL.

Tags
see
addOrderBy()
Return values
$this

the query object itself

orFilterWhere()

Adds an additional WHERE condition to the existing one but ignores [[isEmpty()|empty operands]].

public orFilterWhere(array<string|int, mixed> $condition) : $this

The new condition and the existing one will be joined using the 'OR' operator.

This method is similar to [[orWhere()]]. The main difference is that this method will remove [[isEmpty()|empty query operands]]. As a result, this method is best suited for building query conditions based on filter values entered by users.

Parameters
$condition : array<string|int, mixed>

the new WHERE condition. Please refer to [[where()]] on how to specify this parameter.

Tags
see
filterWhere()
see
andFilterWhere()
Return values
$this

the query object itself

orWhere()

Adds an additional WHERE condition to the existing one.

public orWhere(string|array<string|int, mixed>|ExpressionInterface $condition) : $this

The new condition and the existing one will be joined using the 'OR' operator.

Parameters
$condition : string|array<string|int, mixed>|ExpressionInterface

the new WHERE condition. Please refer to [[where()]] on how to specify this parameter.

Tags
see
where()
see
andWhere()
Return values
$this

the query object itself

where()

Sets the WHERE part of the query.

public where(string|array<string|int, mixed>|ExpressionInterface $condition) : $this

See [[QueryInterface::where()]] for detailed documentation.

Parameters
$condition : string|array<string|int, mixed>|ExpressionInterface

the conditions that should be put in the WHERE part.

Tags
see
andWhere()
see
orWhere()
Return values
$this

the query object itself

filterCondition()

Removes [[isEmpty()|empty operands]] from the given query condition.

protected filterCondition(array<string|int, mixed> $condition) : array<string|int, mixed>
Parameters
$condition : array<string|int, mixed>

the original condition

Tags
throws
NotSupportedException

if the condition operator is not supported

Return values
array<string|int, mixed>

the condition with [[isEmpty()|empty operands]] removed.

isEmpty()

Returns a value indicating whether the give value is "empty".

protected isEmpty(mixed $value) : bool

The value is considered "empty", if one of the following conditions is satisfied:

  • it is null,
  • an empty string (''),
  • a string containing only whitespace characters,
  • or an empty array.
Parameters
$value : mixed
Return values
bool

if the value is empty

normalizeOrderBy()

Normalizes format of ORDER BY data.

protected normalizeOrderBy(array<string|int, mixed>|string|ExpressionInterface|null $columns) : array<string|int, mixed>
Parameters
$columns : array<string|int, mixed>|string|ExpressionInterface|null

the columns value to normalize. See [[orderBy]] and [[addOrderBy]].

Return values
array<string|int, mixed>

        
On this page

Search results