PHP Interface yii\db\ActiveQueryInterface

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.
Since: 2.0
Author: Qiang Xue ([email protected])
Author: Carsten Brandt ([email protected])
Inheritance: extends yii\db\QueryInterface
Mostra file Open project: yiisoft/yii2 Interface Usage Examples

Public Methods

Method Description
asArray ( boolean $value = true ) Sets the [[asArray]] property.
findFor ( string $name, yii\db\ActiveRecordInterface $model ) : mixed Finds the related records for the specified primary record.
indexBy ( string | callable $column ) Sets the [[indexBy]] property.
one ( Connection $db = null ) : yii\db\ActiveRecordInterface | array | null Executes query and returns a single row of result.
via ( string $relationName, callable $callable = null ) Specifies the relation associated with the junction table for use in relational query.
with ( ) Specifies the relations with which this query should be performed.

Method Details

asArray() public method

Sets the [[asArray]] property.
public asArray ( boolean $value = true )
$value boolean whether to return the query results in terms of arrays instead of Active Records.

findFor() public method

This method is invoked when a relation of an ActiveRecord is being accessed in a lazy fashion.
public findFor ( string $name, yii\db\ActiveRecordInterface $model ) : mixed
$name string the relation name
$model yii\db\ActiveRecordInterface the primary model
return mixed the related record(s)

indexBy() public method

Sets the [[indexBy]] property.
public indexBy ( string | callable $column )
$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 or model data. The signature of the callable should be: ```php // $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 } ```

one() public method

Executes query and returns a single row of result.
public one ( Connection $db = null ) : yii\db\ActiveRecordInterface | array | null
$db Connection the DB connection used to create the DB command. If `null`, the DB connection returned by [[ActiveQueryTrait::$modelClass|modelClass]] will be used.
return yii\db\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.

via() public method

Specifies the relation associated with the junction table for use in relational query.
public via ( string $relationName, callable $callable = null )
$relationName string the relation name. This refers to a relation declared in the [[ActiveRelationTrait::primaryModel|primaryModel]] of the relation.
$callable callable a 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.

with() public method

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 [[ActiveQueryTrait::modelClass|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: php 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();
public with ( )