Class yii\mongodb\ActiveQuery
Inheritance | yii\mongodb\ActiveQuery » yii\mongodb\Query » yii\base\Component » yii\base\BaseObject |
---|---|
Implements | yii\base\Configurable, yii\db\ActiveQueryInterface, yii\db\QueryInterface |
Uses Traits | yii\db\ActiveQueryTrait, yii\db\ActiveRelationTrait, yii\db\QueryTrait |
Available since version | 2.0 |
Source Code | https://github.com/yiisoft/yii2-mongodb/blob/master/ActiveQuery.php |
ActiveQuery represents a Mongo 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\mongodb\ActiveRecord::find(). Relational queries are created by yii\mongodb\ActiveRecord::hasOne() and yii\mongodb\ActiveRecord::hasMany().
Normal Query ¶
ActiveQuery instances are usually created by yii\mongodb\ActiveRecord::find().
Because ActiveQuery extends from yii\mongodb\Query, one can use query methods, such as where(), orderBy() to customize the query options.
ActiveQuery also provides the following additional query options:
- with(): list of relations that this query should be performed with.
- 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\mongodb\ActiveRecord::hasOne() and yii\mongodb\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 $link which represents the association between columns of different collections; and the multiplicity of the relation is indicated by $multiple.
If a relation involves a junction collection, it may be specified by via(). This methods may only be called in a relational context. Same is true for inverseOf(), which marks a relation as inverse of another relation.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$asArray | boolean | Whether to return each record as an array. | yii\db\ActiveQueryTrait |
$emulateExecution | boolean | Whether to emulate the actual query execution, returning empty or false results. | yii\db\QueryTrait |
$from | string|array | The collection to be selected from. | yii\mongodb\Query |
$indexBy | string|callable | The name of the column by which the query results should be indexed by. | yii\db\QueryTrait |
$inverseOf | string | The name of the relation that is the inverse of this relation. | yii\db\ActiveRelationTrait |
$limit | integer|yii\db\ExpressionInterface | Maximum number of records to be returned. | yii\db\QueryTrait |
$link | array | The columns of the primary and foreign tables that establish a relation. | yii\db\ActiveRelationTrait |
$modelClass | string | The name of the ActiveRecord class. | yii\db\ActiveQueryTrait |
$multiple | boolean | Whether this query represents a relation to more than one record. | yii\db\ActiveRelationTrait |
$offset | integer|yii\db\ExpressionInterface | Zero-based offset from where the records are to be returned. | yii\db\QueryTrait |
$options | array | Cursor options in format: optionKey => optionValue | yii\mongodb\Query |
$orderBy | array | How to sort the query results. | yii\db\QueryTrait |
$primaryModel | yii\db\ActiveRecord | The primary model of a relational query. | yii\db\ActiveRelationTrait |
$select | array | The fields of the results to return. | yii\mongodb\Query |
$via | array|object | The query associated with the junction table. | yii\db\ActiveRelationTrait |
$where | string|array|yii\db\ExpressionInterface | Query condition. | yii\db\QueryTrait |
$with | array | A list of relations that this query should be performed with | yii\db\ActiveQueryTrait |
Public Methods
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | yii\base\Component |
__clone() | Clones internal objects. | yii\db\ActiveRelationTrait |
__construct() | Constructor. | yii\mongodb\ActiveQuery |
__get() | Returns the value of a component property. | yii\base\Component |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\Component |
__set() | Sets the value of a component property. | yii\base\Component |
__unset() | Sets a component property to be null. | yii\base\Component |
addOptions() | Adds additional cursor options. | yii\mongodb\Query |
addOrderBy() | Adds additional ORDER BY columns to the query. | yii\db\QueryTrait |
all() | Executes query and returns all results as an array. | yii\mongodb\ActiveQuery |
andFilterCompare() | Helper method for easy querying on values containing some common operators. | yii\mongodb\Query |
andFilterWhere() | Adds an additional WHERE condition to the existing one but ignores empty operands. | yii\db\QueryTrait |
andWhere() | Adds an additional WHERE condition to the existing one. | yii\db\QueryTrait |
asArray() | Sets the asArray() property. | yii\db\ActiveQueryTrait |
attachBehavior() | Attaches a behavior to this component. | yii\base\Component |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\base\Component |
average() | Returns the average of the specified column values. | yii\mongodb\Query |
batch() | Starts a batch query. | yii\mongodb\Query |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\base\Component |
buildCursor() | Builds the MongoDB cursor for this query. | yii\mongodb\Query |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\Component |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\Component |
className() | Returns the fully qualified name of this class. | yii\base\BaseObject |
column() | Executes the query and returns the first column of the result. | yii\mongodb\Query |
count() | Returns the number of records. | yii\mongodb\Query |
detachBehavior() | Detaches a behavior from the component. | yii\base\Component |
detachBehaviors() | Detaches all behaviors from the component. | yii\base\Component |
distinct() | Returns a list of distinct values for the given column across a collection. | yii\mongodb\Query |
each() | Starts a batch query and retrieves data row by row. | yii\mongodb\Query |
emulateExecution() | Sets whether to emulate query execution, preventing any interaction with data storage. | yii\db\QueryTrait |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\base\Component |
exists() | Returns a value indicating whether the query result contains any row of data. | yii\mongodb\Query |
filterWhere() | Sets the WHERE part of the query but ignores empty operands. | yii\db\QueryTrait |
findFor() | Finds the related records for the specified primary record. | yii\db\ActiveRelationTrait |
findWith() | Finds records corresponding to one or multiple relations and populates them into the primary models. | yii\db\ActiveQueryTrait |
from() | Sets the collection to be selected from. | yii\mongodb\Query |
getBehavior() | Returns the named behavior object. | yii\base\Component |
getBehaviors() | Returns all behaviors attached to this component. | yii\base\Component |
getCollection() | Returns the Mongo collection for this query. | yii\mongodb\ActiveQuery |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\base\Component |
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\Component |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\base\Component |
indexBy() | Sets the indexBy() property. | yii\db\QueryTrait |
init() | Initializes the object. | yii\mongodb\ActiveQuery |
inverseOf() | Sets the name of the relation that is the inverse of this relation. | yii\db\ActiveRelationTrait |
limit() | Sets the LIMIT part of the query. | yii\db\QueryTrait |
max() | Returns the maximum of the specified column values. | yii\mongodb\Query |
min() | Returns the minimum of the specified column values. | yii\mongodb\Query |
modify() | Performs 'findAndModify' query and returns a single row of result. | yii\mongodb\ActiveQuery |
off() | Detaches an existing event handler from this component. | yii\base\Component |
offset() | Sets the OFFSET part of the query. | yii\db\QueryTrait |
on() | Attaches an event handler to an event. | yii\base\Component |
one() | Executes query and returns a single row of result. | yii\mongodb\ActiveQuery |
options() | Sets the cursor options. | yii\mongodb\Query |
orFilterWhere() | Adds an additional WHERE condition to the existing one but ignores empty operands. | yii\db\QueryTrait |
orWhere() | Adds an additional WHERE condition to the existing one. | yii\db\QueryTrait |
orderBy() | Sets the ORDER BY part of the query. | yii\db\QueryTrait |
populate() | Converts the raw query results into the format as specified by this query. | yii\mongodb\ActiveQuery |
populateRelation() | Finds the related records and populates them into the primary models. | yii\db\ActiveRelationTrait |
prepare() | {@inheritdoc} | yii\mongodb\ActiveQuery |
scalar() | Returns the query result as a scalar value. | yii\mongodb\Query |
select() | Sets the list of fields of the results to return. | yii\mongodb\Query |
sum() | Returns the sum of the specified column values. | yii\mongodb\Query |
trigger() | Triggers an event. | yii\base\Component |
via() | Specifies the relation associated with the junction table. | yii\db\ActiveRelationTrait |
where() | Sets the WHERE part of the query. | yii\db\QueryTrait |
with() | Specifies the relations with which this query should be performed. | yii\db\ActiveQueryTrait |
Protected Methods
Method | Description | Defined By |
---|---|---|
aggregate() | Performs the aggregation for the given column. | yii\mongodb\Query |
createModels() | Converts found rows into model instances. | yii\db\ActiveQueryTrait |
fetchRows() | Fetches rows from the given Mongo cursor. | yii\mongodb\Query |
fetchRowsInternal() | yii\mongodb\Query | |
filterCondition() | Removes empty operands from the given query condition. | yii\db\QueryTrait |
isEmpty() | Returns a value indicating whether the give value is "empty". | yii\db\QueryTrait |
normalizeOrderBy() | Normalizes format of ORDER BY data. | yii\db\QueryTrait |
Events
Event | Type | Description | Defined By |
---|---|---|---|
EVENT_INIT | \yii\mongodb\Event | An event that is triggered when the query is initialized via init(). | yii\mongodb\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 query and returns all results as an array.
public array|yii\mongodb\ActiveRecord all ( $db = null ) | ||
$db | yii\mongodb\Connection | The Mongo connection used to execute the query. If null, the Mongo connection returned by $modelClass will be used. |
return | array|yii\mongodb\ActiveRecord | The query results. If the query results in nothing, an empty array will be returned. |
---|
Returns the Mongo collection for this query.
public yii\mongodb\Collection getCollection ( $db = null ) | ||
$db | yii\mongodb\Connection | Mongo connection. |
return | yii\mongodb\Collection | Collection instance. |
---|
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 ( ) |
Performs 'findAndModify' query and returns a single row of result.
Warning: in case 'new' option is set to 'false' (which is by default) usage of this method may lead to unexpected behavior at some Active Record features, because object will be populated by outdated data.
public yii\mongodb\ActiveRecord|array|null modify ( $update, $options = [], $db = null ) | ||
$update | array | Update criteria |
$options | array | List of options in format: optionName => optionValue. |
$db | yii\mongodb\Connection | The Mongo connection used to execute the query. |
return | yii\mongodb\ActiveRecord|array|null | The original document, or the modified document when $options['new'] is set. 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. |
---|
Executes query and returns a single row of result.
public yii\mongodb\ActiveRecord|array|null one ( $db = null ) | ||
$db | yii\mongodb\Connection | The Mongo connection used to execute the query. If null, the Mongo connection returned by $modelClass will be used. |
return | yii\mongodb\ActiveRecord|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. |
---|
Converts the raw query results into the format as specified by this query.
This method is internally used to convert the data fetched from MongoDB into the format as required by this query.
public array populate ( $rows ) | ||
$rows | array | The raw query result from MongoDB |
return | array | The converted query result |
---|
{@inheritdoc}
public void prepare ( ) |
Event Details
An event that is triggered when the query is initialized via init().