Represents a db query associated with an Active Record class.
An ActiveQuery can be a normal query or be used in a relational context.
ActiveQuery instances are usually created by {@see \Yiisoft\ActiveRecord\findOne()}, {@see \Yiisoft\ActiveRecord\findBySql()}, {@see \Yiisoft\ActiveRecord\findAll()}.
Relational queries are created by {@see \Yiisoft\ActiveRecord\ActiveRecord::hasOne()} and {@see \Yiisoft\ActiveRecord\ActiveRecord::hasMany()}.
Normal Query ¶
ActiveQuery mainly provides the following methods to retrieve the query results:
- {@see \Yiisoft\ActiveRecord\one()}: returns a single record populated with the first row of data.
- {@see \Yiisoft\ActiveRecord\all()}: returns all records based on the query results.
- {@see \Yiisoft\ActiveRecord\count()}: returns the number of records.
- {@see \Yiisoft\ActiveRecord\sum()}: returns the sum over the specified column.
- {@see \Yiisoft\ActiveRecord\average()}: returns the average over the specified column.
- {@see \Yiisoft\ActiveRecord\min()}: returns the min over the specified column.
- {@see \Yiisoft\ActiveRecord\max()}: returns the max over the specified column.
- {@see \Yiisoft\ActiveRecord\scalar()}: returns the value of the first column in the first row of the query result.
- {@see \Yiisoft\ActiveRecord\column()}: returns the value of the first column in the query result.
- {@see \Yiisoft\ActiveRecord\exists()}: returns a value indicating whether the query result has data or not.
Because ActiveQuery extends from {@see \Yiisoft\Db\Query\Query}, one can use query methods, such as {@see \Yiisoft\ActiveRecord\where()}, {@see \Yiisoft\ActiveRecord\orderBy()} to
customize the query options.
ActiveQuery also provides the following more query options:
- {@see \Yiisoft\ActiveRecord\with()}: list of relations that this query should be performed with.
- {@see \Yiisoft\ActiveRecord\joinWith()}: reuse a relation query definition to add a join to a query.
- {@see \Yiisoft\ActiveRecord\indexBy()}: the name of the column by which the query result should be indexed.
- {@see \Yiisoft\ActiveRecord\asArray()}: whether to return each record as an array.
These options can be configured using methods of the same name. For example:
$customerQuery = Customer::query();
$query = $customerQuery->with('orders')->asArray()->all();
Relational query ¶
In relational context, ActiveQuery represents a relation between two Active Record classes.
Relational ActiveQuery instances are usually created by calling {@see \Yiisoft\ActiveRecord\ActiveRecord::hasOne()} and
{@see \Yiisoft\ActiveRecord\ActiveRecord::hasMany()}. An Active Record class declares a relation by defining a getter method which calls
one of the above methods and returns the created ActiveQuery object.
A relation is specified by {@see \Yiisoft\ActiveRecord\link()} which represents the association between columns of different tables; and
the multiplicity of the relation is indicated by {@see \Yiisoft\ActiveRecord\multiple()}.
If a relation involves a junction table, it may be specified by {@see \Yiisoft\ActiveRecord\via()} or {@see \Yiisoft\ActiveRecord\viaTable()} method.
These methods may only be called in a relational context. The same is true for {@see \Yiisoft\ActiveRecord\inverseOf()}, which marks a relation
as inverse of another relation and {@see \Yiisoft\ActiveRecord\onCondition()} which adds a condition that is to be added to relational
query join condition.
Signup or Login in order to comment.