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\Trait\RepositoryTrait::findOne()},
{@see \Yiisoft\ActiveRecord\Trait\RepositoryTrait::findBySql()}, {@see \Yiisoft\ActiveRecord\Trait\RepositoryTrait::findAll()}.
Relational queries are created by {@see \Yiisoft\ActiveRecord\ActiveRecordInterface::hasOne()} and {@see \Yiisoft\ActiveRecord\ActiveRecordInterface::hasMany()}.
Normal Query ¶
ActiveQuery mainly provides the following methods to retrieve the query results:
- {@see \Yiisoft\Db\Query\Query::one()}: returns a single record populated with the first row of data.
- {@see \Yiisoft\Db\Query\Query::all()}: returns all records based on the query results.
- {@see \Yiisoft\Db\Query\Query::count()}: returns the number of records.
- {@see \Yiisoft\Db\Query\Query::sum()}: returns the sum over the specified column.
- {@see \Yiisoft\Db\Query\Query::average()}: returns the average over the specified column.
- {@see \Yiisoft\Db\Query\Query::min()}: returns the min over the specified column.
- {@see \Yiisoft\Db\Query\Query::max()}: returns the max over the specified column.
- {@see \Yiisoft\Db\Query\Query::scalar()}: returns the value of the first column in the first row of the query result.
- {@see \Yiisoft\Db\Query\Query::column()}: returns the value of the first column in the query result.
- {@see \Yiisoft\Db\Query\Query::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\Db\Query\Query::where()},
{@see \Yiisoft\Db\Query\Query::orderBy()} to customize the query options.
ActiveQuery also provides the following more query options:
- {@see \Yiisoft\ActiveRecord\ActiveQuery::with()}: list of relations that this query should be performed with.
- {@see \Yiisoft\ActiveRecord\ActiveQuery::joinWith()}: reuse a relation query definition to add a join to a query.
- {@see \Yiisoft\Db\Query\Query::indexBy()}: the name of the column by which the query result should be indexed.
- {@see \Yiisoft\ActiveRecord\ActiveQuery::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\ActiveRecordInterface::hasOne()} and
{@see \Yiisoft\ActiveRecord\ActiveRecordInterface::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\ActiveQuery::link()} which represents the association between columns
of different tables; and the multiplicity of the relation is indicated by {@see \Yiisoft\ActiveRecord\ActiveQuery::multiple()}.
If a relation involves a junction table, it may be specified by {@see \Yiisoft\ActiveRecord\ActiveQuery::via()}
or {@see \Yiisoft\ActiveRecord\ActiveQuery::viaTable()} method.
These methods may only be called in a relational context. The same is true for
{@see \Yiisoft\ActiveRecord\ActiveQuery::inverseOf()}, which marks a relation as inverse of another relation
and {@see \Yiisoft\ActiveRecord\ActiveQuery::on()} which adds a condition that is to be added to relational
query join condition.
Signup or Login in order to comment.