Skip to content
PHP Ecosystem
Explore

ActiveQueryInterface

API reference › yii › db › ActiveQueryInterface

interfaceyii\db\ActiveQueryInterface
ExtendsQueryInterface
Since2.0
Sourceframework/db/ActiveQueryInterface.php
Implemented by 1 type

ActiveQueryInterface defines the common interface to be implemented by active record query classes.

That are methods for either normal queries that return active records but also relational queries in which the query represents a relation between two active record classes and will return related records only.

A class implementing this interface should also use ActiveQueryTrait and ActiveRelationTrait.

MethodDescriptionDefined by
addOrderBy() abstractAdds additional ORDER BY columns to the query.QueryInterface
all() abstractExecutes the query and returns all results as an array.QueryInterface
andFilterWhere() abstractAdds an additional WHERE condition to the existing one ignoring empty parameters.QueryInterface
andWhere() abstractAdds an additional WHERE condition to the existing one.QueryInterface
asArray() abstractSets the asArray() property.ActiveQueryInterface
count() abstractReturns the number of records.QueryInterface
emulateExecution() abstractSets whether to emulate query execution, preventing any interaction with data storage.QueryInterface
exists() abstractReturns a value indicating whether the query result contains any row of data.QueryInterface
filterWhere() abstractSets the WHERE part of the query ignoring empty parameters.QueryInterface
findFor() abstractFinds the related records for the specified primary record.ActiveQueryInterface
indexBy() abstractSets the indexBy() property.ActiveQueryInterface
limit() abstractSets the LIMIT part of the query.QueryInterface
offset() abstractSets the OFFSET part of the query.QueryInterface
one() abstractExecutes query and returns a single row of result.ActiveQueryInterface
orderBy() abstractSets the ORDER BY part of the query.QueryInterface
orFilterWhere() abstractAdds an additional WHERE condition to the existing one ignoring empty parameters.QueryInterface
orWhere() abstractAdds an additional WHERE condition to the existing one.QueryInterface
via() abstractSpecifies the relation associated with the junction table for use in relational query.ActiveQueryInterface
where() abstractSets the WHERE part of the query.QueryInterface
with() abstractSpecifies the relations with which this query should be performed.ActiveQueryInterface
abstract public function asArray(bool $value = true): $this

Sets the asArray() property.

Parameters

ParameterTypeDescription
$valueboolWhether to return the query results in terms of arrays instead of Active Records.

Returns

$this: The query object itself

abstract public function findFor(string $name, yii\db\ActiveRecordInterface $model): mixed

Finds the related records for the specified primary record.

This method is invoked when a relation of an ActiveRecord is being accessed in a lazy fashion.

Parameters

ParameterTypeDescription
$namestringThe relation name
$modelActiveRecordInterfaceThe primary model

Returns

mixed: The related record(s)

abstract public function indexBy(string|callable $column): $this

Sets the indexBy() property.

Parameters

ParameterTypeDescription
$columnstring|callableThe 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 or model data. The signature of the callable should be: // $model is an AR instance when asArray is false, // or an array of column values when asArray is true. function ($model) { // return the index value corresponding to $model }

Returns

$this: The query object itself

abstract public function one(yii\db\Connection|null $db = null): yii\db\ActiveRecordInterface|array|null

Executes query and returns a single row of result.

Parameters

ParameterTypeDescription
$dbConnection|nullThe DB connection used to create the DB command. If null, the DB connection returned by modelClass will be used.

Returns

ActiveRecordInterface|array|null: A single row of query result. Depending on the setting of asArray(), the query result may be either an array or an ActiveRecord object. null will be returned if the query results in nothing.

abstract public function via(string $relationName, callable|null $callable = null): $this

Specifies the relation associated with the junction table for use in relational query.

Parameters

ParameterTypeDescription
$relationNamestringThe relation name. This refers to a relation declared in the primaryModel of the relation.
$callablecallable|nullA PHP callback for customizing the relation associated with the junction table. Its signature should be function($query), where $query is the query to be customized.

Returns

$this: The relation object itself.

abstract public function with(): $this

Specifies the relations with which this query should be performed.

The parameters to this method can be either one or multiple strings, or a single array of relation names and the optional callbacks to customize the relations.

A relation name can refer to a relation defined in modelClass or a sub-relation that stands for a relation of a related record. For example, orders.address means the address relation defined in the model class corresponding to the orders relation.

The following are some usage examples:

// find customers together with their orders and country
Customer::find()->with('orders', 'country')->all();
// find customers together with their orders and the orders' shipping address
Customer::find()->with('orders.address')->all();
// find customers together with their country and orders of status 1
Customer::find()->with([
'orders' => function (\yii\db\ActiveQuery $query) {
$query->andWhere('status = 1');
},
'country',
])->all();

Returns

$this: The query object itself