Craft 3 Class Reference

Class craft\db\Query

Inheritance
craft\db\Query » yii\db\Query » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable, yii\db\ExpressionInterface, yii\db\QueryInterface
Uses Traits
yii\db\QueryTrait
Subclasses
craft\elements\db\AssetQuery, craft\elements\db\CategoryQuery, craft\elements\db\ElementQuery, craft\elements\db\EntryQuery, craft\elements\db\GlobalSetQuery, craft\elements\db\MatrixBlockQuery, craft\elements\db\TagQuery, craft\elements\db\UserQuery
Available since version
3.0
Source Code
https://github.com/craftcms/cms/blob/master/src/db/Query.php

Class Query

Public Properties
Property Type Description Defined By
$distinct boolean Whether to select distinct rows of data only. yii\db\Query
$emulateExecution boolean Whether to emulate the actual query execution, returning empty or false results. yii\db\QueryTrait
$from array The table(s) to be selected from. yii\db\Query
$groupBy array How to group the query results. yii\db\Query
$having string, array, yii\db\ExpressionInterface The condition to be applied in the GROUP BY clause. yii\db\Query
$indexBy string, callable The name of the column by which the query results should be indexed by. yii\db\QueryTrait
$join array How to join with other tables. yii\db\Query
$limit integer, yii\db\ExpressionInterface Maximum number of records to be returned. yii\db\QueryTrait
$offset integer, yii\db\ExpressionInterface Zero-based offset from where the records are to be returned. yii\db\QueryTrait
$orderBy array How to sort the query results. yii\db\QueryTrait
$params array List of query parameter values indexed by parameter placeholders. yii\db\Query
$queryCacheDependency yii\caching\Dependency The dependency to be associated with the cached query result for this query yii\db\Query
$queryCacheDuration integer, true The default number of seconds that query results can remain valid in cache. yii\db\Query
$select array The columns being selected. yii\db\Query
$selectOption string Additional option that should be appended to the 'SELECT' keyword. yii\db\Query
$union array This is used to construct the UNION clause(s) in a SQL statement. yii\db\Query
$where string, array Query condition. yii\db\QueryTrait
Public Methods
Method Description Defined By
__call() Calls the named method which is not a class method. yii\base\BaseObject
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__toString() Returns the SQL representation of Query yii\db\Query
__unset() Sets an object property to null. yii\base\BaseObject
addGroupBy() Adds additional group-by columns to the existing ones. yii\db\Query
addOrderBy() Adds additional ORDER BY columns to the query. yii\db\QueryTrait
addParams() Adds additional parameters to be bound to the query. yii\db\Query
addSelect() Add more columns to the SELECT part of the query. yii\db\Query
all() Executes the query and returns all results as an array. craft\db\Query
andFilterCompare() Adds a filtering condition for a specific column and allow the user to choose a filter operator. yii\db\Query
andFilterHaving() Adds an additional HAVING condition to the existing one but ignores empty operands. yii\db\Query
andFilterWhere() Adds an additional WHERE condition to the existing one but ignores empty operands. yii\db\QueryTrait
andHaving() Adds an additional HAVING condition to the existing one. yii\db\Query
andWhere() Adds an additional WHERE condition to the existing one. craft\db\Query
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
average() Returns the average of the specified column values. yii\db\Query
batch() Starts a batch query. yii\db\Query
behaviors() Returns a list of behaviors that this component should behave as. craft\db\Query
cache() Enables query cache for this Query. yii\db\Query
canGetProperty() Returns a value indicating whether a property can be read. yii\base\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
className() Returns the fully qualified name of this class. yii\base\BaseObject
column() Executes the query and returns the first column of the result. craft\db\Query
count() Returns the number of records. yii\db\Query
create() Creates a new Query object and copies its property values from an existing one. yii\db\Query
createCommand() Creates a DB command that can be used to execute this query. yii\db\Query
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
distinct() Sets the value indicating whether to SELECT DISTINCT or not. yii\db\Query
each() Starts a batch query and retrieves data row by row. yii\db\Query
emulateExecution() Sets whether to emulate query execution, preventing any interaction with data storage. yii\db\QueryTrait
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
exists() Returns a value indicating whether the query result contains any row of data. craft\db\Query
filterHaving() Sets the HAVING part of the query but ignores empty operands. yii\db\Query
filterWhere() Sets the WHERE part of the query but ignores empty operands. yii\db\QueryTrait
from() Sets the FROM part of the query. yii\db\Query
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getRawSql() Shortcut for createCommand()->getRawSql(). craft\db\Query
getTablesUsedInFrom() Returns table names used in from() indexed by aliases. yii\db\Query
groupBy() Sets the GROUP BY part of the query. yii\db\Query
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
having() Sets the HAVING part of the query. yii\db\Query
indexBy() Sets the indexBy() property. yii\db\QueryTrait
init() Initializes the object. craft\db\Query
innerJoin() Appends an INNER JOIN part to the query. yii\db\Query
isJoined() Returns whether a given table has been joined in this query. craft\db\Query
join() Appends a JOIN part to the query. yii\db\Query
leftJoin() Appends a LEFT OUTER JOIN part to the query. yii\db\Query
limit() Sets the LIMIT part of the query. yii\db\QueryTrait
max() Returns the maximum of the specified column values. yii\db\Query
min() Returns the minimum of the specified column values. yii\db\Query
noCache() Disables query cache for this Query. yii\db\Query
nth() Executes the query and returns a single row of result at a given offset. craft\db\Query
off() Detaches an existing event handler from this component. yii\base\Component
offset() Sets the OFFSET part of the query. yii\db\QueryTrait
on() Attaches an event handler to an event. yii\base\Component
one() Executes the query and returns a single row of result. craft\db\Query
orFilterHaving() Adds an additional HAVING condition to the existing one but ignores empty operands. yii\db\Query
orFilterWhere() Adds an additional WHERE condition to the existing one but ignores empty operands. yii\db\QueryTrait
orHaving() Adds an additional HAVING condition to the existing one. yii\db\Query
orWhere() Adds an additional WHERE condition to the existing one. craft\db\Query
orderBy() Sets the ORDER BY part of the query. yii\db\QueryTrait
pairs() Executes the query and returns the first two columns in the results as key/value pairs. craft\db\Query
params() Sets the parameters to be bound to the query. yii\db\Query
populate() Converts the raw query results into the format as specified by this query. yii\db\Query
prepare() Prepares for building SQL. yii\db\Query
rightJoin() Appends a RIGHT OUTER JOIN part to the query. yii\db\Query
scalar() Returns the query result as a scalar value. craft\db\Query
select() Sets the SELECT part of the query. yii\db\Query
sum() Returns the sum of the specified column values. yii\db\Query
trigger() Triggers an event. yii\base\Component
union() Appends a SQL statement using UNION operator. yii\db\Query
where() Sets the WHERE part of the query. craft\db\Query
Protected Methods
Method Description Defined By
cleanUpTableNames() Clean up table names and aliases Both aliases and names are enclosed into {{ and }}. yii\db\Query
filterCondition() Removes empty operands from the given query condition. yii\db\QueryTrait
getUnaliasedColumnsFromSelect() yii\db\Query
getUniqueColumns() Returns unique column names excluding duplicates. yii\db\Query
isEmpty() Returns a value indicating whether the give value is "empty". yii\db\QueryTrait
normalizeOrderBy() Normalizes format of ORDER BY data. yii\db\QueryTrait
queryScalar() Queries a scalar value by setting select() first. craft\db\Query
setCommandCache() Sets $command cache, if this query has enabled caching. yii\db\Query
Events
Event Type Description Defined By
EVENT_DEFINE_BEHAVIORS craft\events\DefineBehaviorsEvent The event that is triggered when defining the class behaviors craft\db\Query
EVENT_INIT yii\base\Event The event that is triggered after the query's init cycle craft\db\Query

Method Details

all() public method #

Executes the query and returns all results as an array.

public array all ( $db null )
$db yii\db\Connection The database connection used to generate the SQL statement. If this parameter is not given, the db application component will be used.
return array The query results. If the query results in nothing, an empty array will be returned.

andWhere() public method #

Adds an additional WHERE condition to the existing one.

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

public $this andWhere ( $condition, $params = [] )
$condition string, array, yii\db\ExpressionInterface The new WHERE condition. Please refer to where() on how to specify this parameter.
$params array The parameters (name => value) to be bound to the query.
return $this The query object itself

behaviors() public method #

Returns a list of behaviors that this component should behave as.

Child classes may override this method to specify the behaviors they want to behave as.

The return value of this method should be an array of behavior objects or configurations indexed by behavior names. A behavior configuration can be either a string specifying the behavior class or an array of the following structure:

'behaviorName' => [
    'class' => 'BehaviorClass',
    'property1' => 'value1',
    'property2' => 'value2',
]

Note that a behavior class must extend from \craft\db\Behavior. Behaviors can be attached using a name or anonymously. When a name is used as the array key, using this name, the behavior can later be retrieved using getBehavior() or be detached using detachBehavior(). Anonymous behaviors can not be retrieved or detached.

Behaviors declared in this method will be attached to the component automatically (on demand).

public array behaviors ( )
return array The behavior configurations.

column() public method #

Executes the query and returns the first column of the result.

public array column ( $db null )
$db yii\db\Connection The database connection used to generate the SQL statement. If this parameter is not given, the db application component will be used.
return array The first column of the query result. An empty array is returned if the query results in nothing.

exists() public method #

Returns a value indicating whether the query result contains any row of data.

public boolean exists ( $db null )
$db yii\db\Connection The database connection used to generate the SQL statement. If this parameter is not given, the db application component will be used.
return boolean Whether the query result contains any row of data.

getRawSql() public method #

Shortcut for createCommand()->getRawSql().

See also:

public string getRawSql ( yii\db\Connection $db null )
$db yii\db\Connection, null The database connection used to generate the SQL statement. If this parameter is not given, the db application component will be used.

init() public method #

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

public void init ( )

isJoined() public method #

Returns whether a given table has been joined in this query.

public boolean isJoined ( \craft\db\string $table )
$table string

nth() public method #

Executes the query and returns a single row of result at a given offset.

public array, null nth ( \craft\db\int $n, yii\db\Connection $db null )
$n integer The offset of the row to return. If offset() is set, $offset will be added to it.
$db yii\db\Connection, null The database connection used to generate the SQL statement. If this parameter is not given, the db application component will be used.
return array, null The row (in terms of an array) of the query result. Null is returned if the query results in nothing.

one() public method #

Executes the query and returns a single row of result.

public array, null one ( $db null )
$db yii\db\Connection The database connection used to generate the SQL statement. If this parameter is not given, the db application component will be used.
return array, null The first row (in terms of an array) of the query result. Null is returned if the query results in nothing.

orWhere() public method #

Adds an additional WHERE condition to the existing one.

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

public $this orWhere ( $condition, $params = [] )
$condition string, array, yii\db\ExpressionInterface The new WHERE condition. Please refer to where() on how to specify this parameter.
$params array The parameters (name => value) to be bound to the query.
return $this The query object itself

pairs() public method #

Executes the query and returns the first two columns in the results as key/value pairs.

public array pairs ( yii\db\Connection $db null )
$db yii\db\Connection, null The database connection used to execute the query. If this parameter is not given, the db application component will be used.
return array The query results. If the query results in nothing, an empty array will be returned.
throws yii\base\Exception if less than two columns were selected

queryScalar() protected method #

Queries a scalar value by setting select() first.

Restores the value of select to make this query reusable.

protected boolean, string queryScalar ( $selectExpression, $db )
$selectExpression string, yii\db\ExpressionInterface
$db yii\db\Connection, null

scalar() public method #

Returns the query result as a scalar value.

The value returned will be the first column in the first row of the query results.

public string, null, false scalar ( $db null )
$db yii\db\Connection The database connection used to generate the SQL statement. If this parameter is not given, the db application component will be used.
return string, null, false The value of the first column in the first row of the query result. False is returned if the query result is empty.

where() public method #

Sets the WHERE part of the query.

The method requires a $condition parameter, and optionally a $params parameter specifying the values to be bound to the query.

The $condition parameter should be either a string (e.g. 'id=1') or an array.

{@inheritdoc}

public $this where ( $condition, $params = [] )
$condition string, array, yii\db\ExpressionInterface The conditions that should be put in the WHERE part.
$params array The parameters (name => value) to be bound to the query.
return $this The query object itself

Event Details

EVENT_DEFINE_BEHAVIORS event of type craft\events\DefineBehaviorsEvent #

The event that is triggered when defining the class behaviors

See also behaviors().

EVENT_INIT event of type yii\base\Event #

The event that is triggered after the query's init cycle

See also init().