Skip to content

ActiveQuery ​

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

Active Query class.

View source

Public Properties ​

PropertyDescription
aliasstring – The table alias for modelClass.
asArrayboolean – Whether to return each record as an array.
behaviorsyii\base\Behavior – List of behaviors attached to this component.
distinctboolean – Whether to select distinct rows of data only.
emulateExecutionboolean – Whether to emulate the actual query execution, returning empty or false results.
fromarray, null – The table(s) to be selected from.
groupByarray, null – How to group the query results.
havingstring, array, yii\db\ExpressionInterface, null – The condition to be applied in the GROUP BY clause.
indexBystring, callable, null – The name of the column by which the query results should be indexed by.
inverseOfstring – The name of the relation that is the inverse of this relation.
joinarray, null – How to join with other tables.
joinWitharray, null – A list of relations that this query should be joined with
limitinteger, yii\db\ExpressionInterface, null – Maximum number of records to be returned.
linkarray – The columns of the primary and foreign tables that establish a relation.
modelClassstring – The name of the ActiveRecord class.
multipleboolean – Whether this query represents a relation to more than one record.
offsetinteger, yii\db\ExpressionInterface, null – Zero-based offset from where the records are to be returned.
onstring, array, null – The join condition to be used when this query is used in a relational context.
orderByarray, null – How to sort the query results.
paramsarray, null – List of query parameter values indexed by parameter placeholders.
primaryModelyii\db\ActiveRecord – The primary model of a relational query.
queryCacheDependencyyii\caching\Dependency, null – The dependency to be associated with the cached query result for this query
queryCacheDurationinteger, boolean, null – The default number of seconds that query results can remain valid in cache.
selectarray, null – The columns being selected.
selectOptionstring, null – Additional option that should be appended to the 'SELECT' keyword.
sqlstring, null – The SQL statement to be executed for retrieving AR records.
tablesUsedInFromstring[] – Table names indexed by aliases.
unionarray, null – This is used to construct the UNION clause(s) in a SQL statement.
viaarray, object – The query associated with the junction table.
wherestring, array, yii\db\ExpressionInterface, null – Query condition.
witharray – A list of relations that this query should be performed with
withQueriesarray, null – This is used to construct the WITH section in a SQL query.

alias ​

Type
string
Default value
null

The table alias for modelClass.

View source

Public Methods ​

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

collect() ​

Since
4.3.0

Executes the query and returns all results as a collection.

View source

Arguments ​

  • $db (yii\db\Connection, null) – The database connection used to generate the SQL statement. If null, the DB connection returned by modelClass will be used.

Returns ​

\Illuminate\Support\Collection – A collection of the resulting records.

getAlias() ​

Returns the table alias for modelClass.

View source

Returns ​

string

Protected Methods ​

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