Class yii\mongodb\ActiveQuery

Inheritanceyii\mongodb\ActiveQuery » yii\mongodb\Query » yii\base\Component
Implementsyii\db\ActiveQueryInterface, yii\db\QueryInterface
Uses Traitsyii\db\ActiveQueryTrait, yii\db\ActiveRelationTrait, yii\db\QueryTrait
Available since version2.0

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 \yii\mongodb\where(), \yii\mongodb\orderBy() to customize the query options.

ActiveQuery also provides the following additional query options:

  • \yii\mongodb\with(): list of relations that this query should be performed with.
  • \yii\mongodb\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 \yii\mongodb\link which represents the association between columns of different collections; and the multiplicity of the relation is indicated by \yii\mongodb\multiple.

If a relation involves a junction collection, it may be specified by \yii\mongodb\via(). This methods may only be called in a relational context. Same is true for \yii\mongodb\inverseOf(), which marks a relation as inverse of another relation.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$from string|array The collection to be selected from. yii\mongodb\Query
$options array Cursor options in format: optionKey => optionValue yii\mongodb\Query
$select array The fields of the results to return. yii\mongodb\Query

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__construct() Constructor. yii\mongodb\ActiveQuery
addOptions() Adds additional cursor options. yii\mongodb\Query
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
average() Returns the average of the specified column values. yii\mongodb\Query
count() Returns the number of records. yii\mongodb\Query
distinct() Returns a list of distinct values for the given column across a collection. yii\mongodb\Query
exists() Returns a value indicating whether the query result contains any row of data. yii\mongodb\Query
from() Sets the collection to be selected from. yii\mongodb\Query
getCollection() Returns the Mongo collection for this query. yii\mongodb\ActiveQuery
init() Initializes the object. yii\mongodb\ActiveQuery
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
one() Executes query and returns a single row of result. yii\mongodb\ActiveQuery
options() Sets the cursor options. yii\mongodb\Query
populate() Converts the raw query results into the format as specified by this query. yii\mongodb\ActiveQuery
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

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
aggregate() Performs the aggregation for the given column. yii\mongodb\Query
buildCursor() Builds the Mongo cursor for this query. yii\mongodb\ActiveQuery
fetchRows() Fetches rows from the given Mongo cursor. yii\mongodb\Query
fetchRowsInternal() yii\mongodb\Query

Events

Hide inherited events

EventTypeDescriptionDefined By
EVENT_INIT \yii\mongodb\Event An event that is triggered when the query is initialized via init(). yii\mongodb\ActiveQuery

Method Details

__construct() public method

Constructor.

public void __construct ( $modelClass, $config = [] )
$modelClass array

The model class associated with this query

$config array

Configurations to be applied to the newly created query object

all() public method

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 \yii\mongodb\modelClass will be used.

return array|yii\mongodb\ActiveRecord

The query results. If the query results in nothing, an empty array will be returned.

buildCursor() protected method

Builds the Mongo cursor for this query.

protected \MongoCursor buildCursor ( $db null )
$db yii\mongodb\Connection

The database connection used to execute the query.

return \MongoCursor

Mongo cursor instance.

getCollection() public method

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.

init() public method

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 ( )
modify() public method

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 \yii\mongodb\asArray, the query result may be either an array or an ActiveRecord object. Null will be returned if the query results in nothing.

one() public method

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 \yii\mongodb\modelClass will be used.

return yii\mongodb\ActiveRecord|array|null

A single row of query result. Depending on the setting of \yii\mongodb\asArray, the query result may be either an array or an ActiveRecord object. Null will be returned if the query results in nothing.

populate() public method

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

Event Details

EVENT_INIT event of type \yii\mongodb\Event

An event that is triggered when the query is initialized via init().