Class yii\redis\ActiveQuery
Inheritance | yii\redis\ActiveQuery » yii\base\Component |
---|---|
Implements | yii\db\ActiveQueryInterface |
Uses Traits | yii\db\ActiveQueryTrait, yii\db\ActiveRelationTrait, yii\db\QueryTrait |
Available since version | 2.0 |
ActiveQuery represents a 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 yii\redis\ActiveRecord::find(). Relational queries are created by yii\redis\ActiveRecord::hasOne() and yii\redis\ActiveRecord::hasMany().
Normal Query
ActiveQuery mainly provides the following methods to retrieve the query results:
- one(): returns a single record populated with the first row of data.
- all(): returns all records based on the query results.
- count(): returns the number of records.
- sum(): returns the sum over the specified column.
- average(): returns the average over the specified column.
- min(): returns the min over the specified column.
- max(): returns the max over the specified column.
- scalar(): returns the value of the first column in the first row of the query result.
- exists(): returns a value indicating whether the query result has data or not.
You can use query methods, such as \yii\redis\where(), \yii\redis\limit() and \yii\redis\orderBy() to customize the query options.
ActiveQuery also provides the following additional query options:
- \yii\redis\with(): list of relations that this query should be performed with.
- \yii\redis\indexBy(): the name of the column by which the query result should be indexed.
- \yii\redis\asArray(): whether to return each record as an array.
These options can be configured using methods of the same name. For example:
$customers = Customer::find()->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 yii\redis\ActiveRecord::hasOne() and yii\redis\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 \yii\redis\link which represents the association between columns of different tables; and the multiplicity of the relation is indicated by \yii\redis\multiple.
If a relation involves a junction table, it may be specified by \yii\redis\via(). This methods may only be called in a relational context. Same is true for \yii\redis\inverseOf(), which marks a relation as inverse of another relation.
Public Methods
Method | Description | Defined By |
---|---|---|
__construct() | Constructor. | yii\redis\ActiveQuery |
all() | Executes the query and returns all results as an array. | yii\redis\ActiveQuery |
average() | Returns the average of the specified column values. | yii\redis\ActiveQuery |
column() | Executes the query and returns the first column of the result. | yii\redis\ActiveQuery |
count() | Returns the number of records. | yii\redis\ActiveQuery |
exists() | Returns a value indicating whether the query result contains any row of data. | yii\redis\ActiveQuery |
init() | Initializes the object. | yii\redis\ActiveQuery |
max() | Returns the maximum of the specified column values. | yii\redis\ActiveQuery |
min() | Returns the minimum of the specified column values. | yii\redis\ActiveQuery |
one() | Executes the query and returns a single row of result. | yii\redis\ActiveQuery |
scalar() | Returns the query result as a scalar value. | yii\redis\ActiveQuery |
sum() | Returns the number of records. | yii\redis\ActiveQuery |
Protected Methods
Method | Description | Defined By |
---|---|---|
executeScript() | Executes a script created by yii\redis\LuaScriptBuilder | yii\redis\ActiveQuery |
Events
Event | Type | Description | Defined By |
---|---|---|---|
EVENT_INIT | \yii\redis\Event | An event that is triggered when the query is initialized via init(). | yii\redis\ActiveQuery |
Method Details
Constructor.
public void __construct ( $modelClass, $config = [] ) | ||
$modelClass | string | The model class associated with this query |
$config | array | Configurations to be applied to the newly created query object |
Executes the query and returns all results as an array.
public array|yii\redis\ActiveRecord[] all ( $db = null ) | ||
$db | yii\redis\Connection | The database connection used to execute the query.
If this parameter is not given, the |
return | array|yii\redis\ActiveRecord[] | The query results. If the query results in nothing, an empty array will be returned. |
---|
Returns the average of the specified column values.
public integer average ( $column, $db = null ) | ||
$column | string | The column name or expression. Make sure you properly quote column names in the expression. |
$db | yii\redis\Connection | The database connection used to execute the query.
If this parameter is not given, the |
return | integer | The average of the specified column values. |
---|
Executes the query and returns the first column of the result.
public array column ( $column, $db = null ) | ||
$column | string | Name of the column to select |
$db | yii\redis\Connection | The database connection used to execute the query.
If this parameter is not given, the |
return | array | The first column of the query result. An empty array is returned if the query results in nothing. |
---|
Returns the number of records.
public integer count ( $q = '*', $db = null ) | ||
$q | string | The COUNT expression. This parameter is ignored by this implementation. |
$db | yii\redis\Connection | The database connection used to execute the query.
If this parameter is not given, the |
return | integer | Number of records |
---|
Executes a script created by yii\redis\LuaScriptBuilder
protected array|boolean|null|string executeScript ( $db, $type, $columnName = null ) | ||
$db | yii\redis\Connection|null | The database connection used to execute the query.
If this parameter is not given, the |
$type | string | The type of the script to generate |
$columnName | string | |
throws | \yii\base\NotSupportedException |
---|
Returns a value indicating whether the query result contains any row of data.
public boolean exists ( $db = null ) | ||
$db | yii\redis\Connection | The database connection used to execute the query.
If this parameter is not given, the |
return | boolean | Whether the query result contains any row of data. |
---|
Initializes the object.
This method is called at the end of the constructor. The default implementation will trigger an EVENT_INIT event. If you override this method, make sure you call the parent implementation at the end to ensure triggering of the event.
public void init ( ) |
Returns the maximum of the specified column values.
public integer max ( $column, $db = null ) | ||
$column | string | The column name or expression. Make sure you properly quote column names in the expression. |
$db | yii\redis\Connection | The database connection used to execute the query.
If this parameter is not given, the |
return | integer | The maximum of the specified column values. |
---|
Returns the minimum of the specified column values.
public integer min ( $column, $db = null ) | ||
$column | string | The column name or expression. Make sure you properly quote column names in the expression. |
$db | yii\redis\Connection | The database connection used to execute the query.
If this parameter is not given, the |
return | integer | The minimum of the specified column values. |
---|
Executes the query and returns a single row of result.
public yii\redis\ActiveRecord|array|null one ( $db = null ) | ||
$db | yii\redis\Connection | The database connection used to execute the query.
If this parameter is not given, the |
return | yii\redis\ActiveRecord|array|null | A single row of query result. Depending on the setting of \yii\redis\asArray, the query result may be either an array or an ActiveRecord object. Null will be returned if the query results in nothing. |
---|
Returns the query result as a scalar value.
The value returned will be the specified attribute in the first record of the query results.
public string scalar ( $attribute, $db = null ) | ||
$attribute | string | Name of the attribute to select |
$db | yii\redis\Connection | The database connection used to execute the query.
If this parameter is not given, the |
return | string | The value of the specified attribute in the first record of the query result. Null is returned if the query result is empty. |
---|
Returns the number of records.
public integer sum ( $column, $db = null ) | ||
$column | string | The column to sum up |
$db | yii\redis\Connection | The database connection used to execute the query.
If this parameter is not given, the |
return | integer | Number of records |
---|
Event Details
An event that is triggered when the query is initialized via init().