W3cubDocs

/Yii 2.0

Class yii\sphinx\ActiveQuery

Inheritance yii\sphinx\ActiveQuery » yii\sphinx\Query » yii\db\Query » yii\base\Component » yii\base\Object
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-sphinx/blob/master/ActiveQuery.php

ActiveQuery represents a Sphinx 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\sphinx\ActiveRecord::find() and yii\sphinx\ActiveRecord::findBySql(). Relational queries are created by yii\sphinx\ActiveRecord::hasOne() and yii\sphinx\ActiveRecord::hasMany().

Normal Query

Because ActiveQuery extends from yii\sphinx\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.
  • indexBy(): the name of the column by which the query result should be indexed.
  • asArray(): whether to return each record as an array.

These options can be configured using methods of the same name. For example:

$articles = Article::find()->with('source')->asArray()->all();

ActiveQuery allows to build the snippets using sources provided by ActiveRecord. You can use snippetByModel() method to enable this. For example:

class Article extends ActiveRecord
{
    public function getSource()
    {
        return $this->hasOne('db', ArticleDb::className(), ['id' => 'id']);
    }

    public function getSnippetSource()
    {
        return $this->source->content;
    }

    ...
}

$articles = Article::find()->with('source')->snippetByModel()->all();

Relational query

In relational context ActiveQuery represents a relation between two Active Record classes.

Relational ActiveQuery instances are usually created by calling yii\sphinx\ActiveRecord::hasOne() and yii\sphinx\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 tables; and the multiplicity of the relation is indicated by $multiple.

If a relation involves a junction table, 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
$behaviors yii\base\Behavior[] List of behaviors attached to this component yii\base\Component
$connection yii\sphinx\Connection Sphinx connection instance yii\sphinx\Query
$distinct boolean Whether to select distinct rows of data only. yii\db\Query
$emulateExecution boolean Whether to emulate the actual query execution, returning empty or false results. yii\db\QueryTrait
$facets array Facet search specifications. yii\sphinx\Query
$from array The table(s) to be selected from. yii\db\Query
$groupBy array How to group the query results. yii\db\Query
$groupLimit integer Groups limit: to return (no more than) N top matches for each group. yii\sphinx\Query
$having string|array|yii\db\Expression The condition to be applied in the GROUP BY clause. yii\db\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
$join array How to join with other tables. yii\db\Query
$limit integer|yii\db\Expression Maximum number of records to be returned. yii\db\QueryTrait
$match string|yii\db\Expression Text, which should be searched in fulltext mode. yii\sphinx\Query
$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\Expression Zero-based offset from where the records are to be returned. yii\db\QueryTrait
$options array Per-query options in format: optionName => optionValue They will compose OPTION clause. yii\sphinx\Query
$orderBy array How to sort the query results. yii\db\QueryTrait
$params array List of query parameter values indexed by parameter placeholders. yii\db\Query
$primaryModel yii\db\ActiveRecord The primary model of a relational query. yii\db\ActiveRelationTrait
$select array The columns being selected. yii\db\Query
$selectOption string Additional option that should be appended to the 'SELECT' keyword. yii\db\Query
$showMeta boolean|string|yii\db\Expression Whether to automatically perform 'SHOW META' query against main one. yii\sphinx\Query
$snippetCallback callable PHP callback, which should be used to fetch source data for the snippets. yii\sphinx\Query
$snippetOptions array Query options for the call snippet. yii\sphinx\Query
$sql string The SQL statement to be executed for retrieving AR records. yii\sphinx\ActiveQuery
$union array This is used to construct the UNION clause(s) in a SQL statement. yii\db\Query
$via array|object The query associated with the junction table. yii\db\ActiveRelationTrait
$where string|array Query condition. yii\db\QueryTrait
$with array A list of relations that this query should be performed with yii\db\ActiveQueryTrait
$within string WITHIN GROUP ORDER BY clause. yii\sphinx\Query

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\sphinx\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
addFacets() Adds additional FACET part of the query. yii\sphinx\Query
addGroupBy() Adds additional group-by columns to the existing ones. yii\db\Query
addOptions() Adds additional query options. yii\sphinx\Query
addOrderBy() Adds additional ORDER BY columns to the query. yii\db\QueryTrait
addParams() Adds additional parameters to be bound to the query. yii\db\Query
addSelect() Add more columns to the SELECT part of the query. yii\db\Query
addWithin() Adds additional WITHIN GROUP ORDER BY columns to the query. yii\sphinx\Query
all() Executes query and returns all results as an array. yii\sphinx\ActiveQuery
andFilterCompare() Adds a filtering condition for a specific column and allow the user to choose a filter operator. yii\db\Query
andFilterHaving() Adds an additional HAVING condition to the existing one but ignores empty operands. yii\db\Query
andFilterWhere() Adds an additional WHERE condition to the existing one but ignores empty operands. yii\db\QueryTrait
andHaving() Adds an additional HAVING condition to the existing one. yii\db\Query
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\db\Query
batch() Starts a batch query. yii\db\Query
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
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\Object
column() Executes the query and returns the first column of the result. yii\db\Query
count() Returns the number of records. yii\db\Query
create() Creates a new Query object and copies its property values from an existing one. yii\sphinx\Query
createCommand() Creates a DB command that can be used to execute this query. yii\sphinx\ActiveQuery
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
distinct() Sets the value indicating whether to SELECT DISTINCT or not. yii\db\Query
each() Starts a batch query and retrieves data row by row. yii\db\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\db\Query
facets() Sets FACET part of the query. yii\sphinx\Query
filterHaving() Sets the HAVING part of the query but ignores empty operands. yii\db\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 FROM part of the query. yii\db\Query
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getConnection() yii\sphinx\Query
groupBy() Sets the GROUP BY part of the query. yii\db\Query
groupLimit() Sets groups limit: to return (no more than) N top matches for each group. yii\sphinx\Query
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
having() Sets the HAVING part of the query. yii\db\Query
indexBy() Sets the indexBy() property. yii\db\QueryTrait
init() Initializes the object. yii\sphinx\ActiveQuery
innerJoin() Appends an INNER JOIN part to the query. yii\sphinx\Query
inverseOf() Sets the name of the relation that is the inverse of this relation. yii\db\ActiveRelationTrait
join() Appends a JOIN part to the query. yii\sphinx\Query
leftJoin() Appends a LEFT OUTER JOIN part to the query. yii\sphinx\Query
limit() Sets the LIMIT part of the query. yii\db\QueryTrait
match() Sets the fulltext query text. This text will be composed into MATCH operator inside the WHERE clause. yii\sphinx\Query
max() Returns the maximum of the specified column values. yii\db\Query
min() Returns the minimum of the specified column values. yii\db\Query
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\sphinx\ActiveQuery
options() Sets the query options. yii\sphinx\Query
orFilterHaving() Adds an additional HAVING condition to the existing one but ignores empty operands. yii\db\Query
orFilterWhere() Adds an additional WHERE condition to the existing one but ignores empty operands. yii\db\QueryTrait
orHaving() Adds an additional HAVING condition to the existing one. yii\db\Query
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
params() Sets the parameters to be bound to the query. yii\db\Query
populate() Converts the raw query results into the format as specified by this query. yii\sphinx\ActiveQuery
populateRelation() Finds the related records and populates them into the primary models. yii\db\ActiveRelationTrait
prepare() Prepares for building SQL. yii\db\Query
rightJoin() Appends a RIGHT OUTER JOIN part to the query. yii\sphinx\Query
scalar() Returns the query result as a scalar value. yii\db\Query
search() Executes the query and returns the complete search result including e.g. hits, facets. yii\sphinx\Query
select() Sets the SELECT part of the query. yii\db\Query
setConnection() yii\sphinx\Query
showMeta() Sets whether to automatically perform 'SHOW META' for the search query. yii\sphinx\Query
snippetByModel() Sets the snippetCallback() to fetchSnippetSourceFromModels(), which allows to fetch the snippet source strings from the Active Record models, using method yii\sphinx\ActiveRecord::getSnippetSource(). yii\sphinx\ActiveQuery
snippetCallback() Sets the PHP callback, which should be used to retrieve the source data for the snippets building. yii\sphinx\Query
snippetOptions() Sets the call snippets query options. yii\sphinx\Query
sum() Returns the sum of the specified column values. yii\db\Query
trigger() Triggers an event. yii\base\Component
union() Appends a SQL statement using UNION operator. yii\db\Query
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
within() Sets the WITHIN GROUP ORDER BY part of the query. yii\sphinx\Query

Protected Methods

Method Description Defined By
callSnippets() Builds a snippets from provided source data. yii\sphinx\ActiveQuery
callSnippetsInternal() Builds a snippets from provided source data by the given index. yii\sphinx\Query
createModels() Converts found rows into model instances yii\db\ActiveQueryTrait
defaultConnection() yii\sphinx\ActiveQuery
fetchSnippetSourceFromModels() Fetches the source for the snippets using yii\sphinx\ActiveRecord::getSnippetSource() method. yii\sphinx\ActiveQuery
fillUpSnippets() Fills the query result rows with the snippets built from source determined by snippetCallback() result. yii\sphinx\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
queryScalar() Queries a scalar value by setting select() first. yii\sphinx\Query

Events

Event Type Description Defined By
EVENT_INIT \yii\sphinx\Event An event that is triggered when the query is initialized via init(). yii\sphinx\ActiveQuery

Property Details

$sql public property

The SQL statement to be executed for retrieving AR records. This is set by yii\sphinx\ActiveRecord::findBySql().

public string $sql = null

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 all ( $db = null )
$db yii\sphinx\Connection

The DB connection used to create the DB command. If null, the DB connection returned by $modelClass will be used.

return array

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

callSnippets() protected method

Builds a snippets from provided source data.

protected array callSnippets ( array $source )
$source array

The source data to extract a snippet from.

return array

Snippets list.

throws yii\base\InvalidCallException

in case match() is not specified.

createCommand() public method

Creates a DB command that can be used to execute this query.

public yii\sphinx\Command createCommand ( $db = null )
$db yii\sphinx\Connection

The DB connection used to create the DB command. If null, the DB connection returned by $modelClass will be used.

return yii\sphinx\Command

The created DB command instance.

defaultConnection() protected method

protected yii\sphinx\Connection defaultConnection ( )
return yii\sphinx\Connection

Default connection value.

fetchSnippetSourceFromModels() protected method

Fetches the source for the snippets using yii\sphinx\ActiveRecord::getSnippetSource() method.

protected array fetchSnippetSourceFromModels ( $models )
$models yii\sphinx\ActiveRecord[]

Raw query result rows.

return array

Snippet source strings

throws yii\base\InvalidCallException

if asArray() enabled.

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 ( )

one() public method

Executes query and returns a single row of result.

public yii\sphinx\ActiveRecord|array|null one ( $db = null )
$db yii\sphinx\Connection

The DB connection used to create the DB command. If null, the DB connection returned by $modelClass will be used.

return yii\sphinx\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.

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 database into the format as required by this query.

public array populate ( $rows )
$rows array

The raw query result from database

return array

The converted query result

snippetByModel() public method

Sets the snippetCallback() to fetchSnippetSourceFromModels(), which allows to fetch the snippet source strings from the Active Record models, using method yii\sphinx\ActiveRecord::getSnippetSource().

For example:

class Article extends ActiveRecord
{
    public function getSnippetSource()
    {
        return file_get_contents('/path/to/source/files/' . $this->id . '.txt');;
    }
}

$articles = Article::find()->snippetByModel()->all();

Warning: this option should NOT be used with asArray() at the same time!

public $this snippetByModel ( )
return $this

The query object itself

Event Details

EVENT_INIT event of type \yii\sphinx\Event

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

© 2008–2017 by Yii Software LLC
Licensed under the three clause BSD license.
http://www.yiiframework.com/doc-2.0/yii-sphinx-activequery.html