ActiveQuery

Type
Class
Namespace
craft\db
Inherits
craft\db\ActiveQuery » yii\db\ActiveQuery (opens new window) » yii\db\Query (opens new window) » yii\base\Component (opens new window) » yii\base\BaseObject (opens new window)
Implements
yii\base\Configurable (opens new window), yii\db\ActiveQueryInterface (opens new window), yii\db\ExpressionInterface (opens new window), yii\db\QueryInterface (opens new window)
Uses traits
yii\db\ActiveQueryTrait (opens new window), yii\db\ActiveRelationTrait (opens new window), yii\db\QueryTrait (opens new window)
Extended by
craft\db\StructuredElementQuery
Since
3.5.13

Active Query class.

View source (opens new window)

# Public Properties

Property Description
alias string (opens new window)
asArray (opens new window) boolean (opens new window) – Whether to return each record as an array.
behaviors (opens new window) yii\base\Behavior (opens new window) – List of behaviors attached to this component
distinct (opens new window) boolean (opens new window) – Whether to select distinct rows of data only.
emulateExecution (opens new window) boolean (opens new window) – Whether to emulate the actual query execution, returning empty or false results.
from (opens new window) array (opens new window) – The table(s) to be selected from.
groupBy (opens new window) array (opens new window) – How to group the query results.
having (opens new window) string (opens new window), array (opens new window), yii\db\ExpressionInterface (opens new window) – The condition to be applied in the GROUP BY clause.
indexBy (opens new window) string (opens new window), callable (opens new window) – The name of the column by which the query results should be indexed by.
inverseOf (opens new window) string (opens new window) – The name of the relation that is the inverse of this relation.
join (opens new window) array (opens new window) – How to join with other tables.
joinWith (opens new window) array (opens new window) – A list of relations that this query should be joined with
limit (opens new window) integer (opens new window), yii\db\ExpressionInterface (opens new window) – Maximum number of records to be returned.
link (opens new window) array (opens new window) – The columns of the primary and foreign tables that establish a relation.
modelClass (opens new window) string (opens new window) – The name of the ActiveRecord class.
multiple (opens new window) boolean (opens new window) – Whether this query represents a relation to more than one record.
offset (opens new window) integer (opens new window), yii\db\ExpressionInterface (opens new window) – Zero-based offset from where the records are to be returned.
on (opens new window) string (opens new window), array (opens new window) – The join condition to be used when this query is used in a relational context.
orderBy (opens new window) array (opens new window) – How to sort the query results.
params (opens new window) array (opens new window) – List of query parameter values indexed by parameter placeholders.
primaryModel (opens new window) yii\db\ActiveRecord (opens new window) – The primary model of a relational query.
queryCacheDependency (opens new window) yii\caching\Dependency (opens new window) – The dependency to be associated with the cached query result for this query
queryCacheDuration (opens new window) integer (opens new window), true (opens new window) – The default number of seconds that query results can remain valid in cache.
select (opens new window) array (opens new window) – The columns being selected.
selectOption (opens new window) string (opens new window) – Additional option that should be appended to the 'SELECT' keyword.
sql (opens new window) string (opens new window) – The SQL statement to be executed for retrieving AR records.
tablesUsedInFrom (opens new window) string (opens new window)[] – Table names indexed by aliases
union (opens new window) array (opens new window) – This is used to construct the UNION clause(s) in a SQL statement.
via (opens new window) array (opens new window), object (opens new window) – The query associated with the junction table.
where (opens new window) string (opens new window), array (opens new window), yii\db\ExpressionInterface (opens new window) – Query condition.
with (opens new window) array (opens new window) – A list of relations that this query should be performed with
withQueries (opens new window) array (opens new window) – This is used to construct the WITH section in a SQL query.

# alias

Type
string (opens new window)
Default value
null
Access
Read-only

View source (opens new window)

# Public Methods

Method Description
__call() (opens new window) Calls the named method which is not a class method.
__clone() (opens new window) Clones internal objects.
__construct() (opens new window) Constructor.
__get() (opens new window) Returns the value of an object property.
__isset() (opens new window) Checks if a property is set, i.e. defined and not null.
__set() (opens new window) Sets value of an object property.
__toString() (opens new window) Returns the SQL representation of Query
__unset() (opens new window) Sets an object property to null.
addGroupBy() (opens new window) Adds additional group-by columns to the existing ones.
addOrderBy() (opens new window) Adds additional ORDER BY columns to the query.
addParams() (opens new window) Adds additional parameters to be bound to the query.
addSelect() (opens new window) Add more columns to the SELECT part of the query.
alias() (opens new window) Define an alias for the table defined in modelClass (opens new window).
all() (opens new window) Executes query and returns all results as an array.
andFilterCompare() (opens new window) Adds a filtering condition for a specific column and allow the user to choose a filter operator.
andFilterHaving() (opens new window) Adds an additional HAVING condition to the existing one but ignores empty operands (opens new window).
andFilterWhere() (opens new window) Adds an additional WHERE condition to the existing one but ignores empty operands (opens new window).
andHaving() (opens new window) Adds an additional HAVING condition to the existing one.
andOnCondition() (opens new window) Adds an additional ON condition to the existing one.
andWhere() (opens new window) Adds an additional WHERE condition to the existing one.
asArray() (opens new window) Sets the asArray() (opens new window) property.
attachBehavior() (opens new window) Attaches a behavior to this component.
attachBehaviors() (opens new window) Attaches a list of behaviors to the component.
average() (opens new window) Returns the average of the specified column values.
batch() (opens new window) Starts a batch query.
behaviors() (opens new window) Returns a list of behaviors that this component should behave as.
cache() (opens new window) Enables query cache for this Query.
canGetProperty() (opens new window) Returns a value indicating whether a property can be read.
canSetProperty() (opens new window) Returns a value indicating whether a property can be set.
className() (opens new window) Returns the fully qualified name of this class.
column() (opens new window) Executes the query and returns the first column of the result.
count() (opens new window) Returns the number of records.
create() (opens new window) Creates a new Query object and copies its property values from an existing one.
createCommand() (opens new window) Creates a DB command that can be used to execute this query.
detachBehavior() (opens new window) Detaches a behavior from the component.
detachBehaviors() (opens new window) Detaches all behaviors from the component.
distinct() (opens new window) Sets the value indicating whether to SELECT DISTINCT or not.
each() (opens new window) Starts a batch query and retrieves data row by row.
emulateExecution() (opens new window) Sets whether to emulate query execution, preventing any interaction with data storage.
ensureBehaviors() (opens new window) Makes sure that the behaviors declared in behaviors() (opens new window) are attached to this component.
exists() (opens new window) Returns a value indicating whether the query result contains any row of data.
filterHaving() (opens new window) Sets the HAVING part of the query but ignores empty operands (opens new window).
filterWhere() (opens new window) Sets the WHERE part of the query but ignores empty operands (opens new window).
findFor() (opens new window) Finds the related records for the specified primary record.
findWith() (opens new window) Finds records corresponding to one or multiple relations and populates them into the primary models.
from() (opens new window) Sets the FROM part of the query.
getAlias() Returns the table alias for modelClass (opens new window).
getBehavior() (opens new window) Returns the named behavior object.
getBehaviors() (opens new window) Returns all behaviors attached to this component.
getTablesUsedInFrom() (opens new window) Returns table names used in from() (opens new window) indexed by aliases.
groupBy() (opens new window) Sets the GROUP BY part of the query.
hasEventHandlers() (opens new window) Returns a value indicating whether there is any handler attached to the named event.
hasMethod() (opens new window) Returns a value indicating whether a method is defined.
hasProperty() (opens new window) Returns a value indicating whether a property is defined.
having() (opens new window) Sets the HAVING part of the query.
indexBy() (opens new window) Sets the indexBy() (opens new window) property.
init() (opens new window) Initializes the object.
innerJoin() (opens new window) Appends an INNER JOIN part to the query.
innerJoinWith() (opens new window) Inner joins with the specified relations.
inverseOf() (opens new window) Sets the name of the relation that is the inverse of this relation.
join() (opens new window) Appends a JOIN part to the query.
joinWith() (opens new window) Joins with the specified relations.
leftJoin() (opens new window) Appends a LEFT OUTER JOIN part to the query.
limit() (opens new window) Determines the number of {elements} that should be returned.
max() (opens new window) Returns the maximum of the specified column values.
min() (opens new window) Returns the minimum of the specified column values.
noCache() (opens new window) Disables query cache for this Query.
off() (opens new window) Detaches an existing event handler from this component.
offset() (opens new window) Determines how many {elements} should be skipped in the results.
on() (opens new window) Attaches an event handler to an event.
onCondition() (opens new window) Sets the ON condition for a relational query.
one() (opens new window) Executes query and returns a single row of result.
orFilterHaving() (opens new window) Adds an additional HAVING condition to the existing one but ignores empty operands (opens new window).
orFilterWhere() (opens new window) Adds an additional WHERE condition to the existing one but ignores empty operands (opens new window).
orHaving() (opens new window) Adds an additional HAVING condition to the existing one.
orOnCondition() (opens new window) Adds an additional ON condition to the existing one.
orWhere() (opens new window) Adds an additional WHERE condition to the existing one.
orderBy() (opens new window) Determines the order that the {elements} should be returned in. (If empty, defaults to {default-order-by}.)
params() (opens new window) Sets the parameters to be bound to the query.
populate() (opens new window) Converts the raw query results into the format as specified by this query.
populateRelation() (opens new window) Finds the related records and populates them into the primary models.
prepare() (opens new window) Prepares for building SQL.
rightJoin() (opens new window) Appends a RIGHT OUTER JOIN part to the query.
scalar() (opens new window) Returns the query result as a scalar value.
select() (opens new window) Sets the SELECT part of the query.
sum() (opens new window) Returns the sum of the specified column values.
trigger() (opens new window) Triggers an event.
union() (opens new window) Appends a SQL statement using UNION operator.
via() (opens new window) Specifies the relation associated with the junction table.
viaTable() (opens new window) Specifies the junction table for a relational query.
where() (opens new window) Sets the WHERE part of the query.
with() (opens new window) Specifies the relations with which this query should be performed.
withQuery() (opens new window) Prepends a SQL statement using WITH syntax.

# getAlias()

Returns the table alias for modelClass (opens new window).

View source (opens new window)

Returns

string (opens new window)

# Protected Methods

Method Description
cleanUpTableNames() (opens new window) Clean up table names and aliases Both aliases and names are enclosed into .
createModels() (opens new window) Converts found rows into model instances.
filterCondition() (opens new window) Removes empty operands (opens new window) from the given query condition.
getPrimaryTableName() (opens new window)
getTableNameAndAlias() (opens new window) Returns the table name and the table alias for modelClass (opens new window).
getUnaliasedColumnsFromSelect() (opens new window)
getUniqueColumns() (opens new window) Returns unique column names excluding duplicates.
isEmpty() (opens new window) Returns a value indicating whether the give value is "empty".
normalizeOrderBy() (opens new window) Normalizes format of ORDER BY data.
normalizeSelect() (opens new window) Normalizes the SELECT columns passed to select() (opens new window) or addSelect() (opens new window).
queryScalar() (opens new window) Queries a scalar value by setting select() (opens new window) first.
setCommandCache() (opens new window) Sets $command cache, if this query has enabled caching.