Class yii\sphinx\ActiveQuery

Inheritanceyii\sphinx\ActiveQuery » yii\sphinx\Query » yii\db\Query
Implementsyii\db\ActiveQueryInterface
Uses Traitsyii\db\ActiveQueryTrait, yii\db\ActiveRelationTrait
Available since version2.0

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

ActiveQuery also provides the following additional query options:

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

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

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$facets array Facet search specifications. yii\sphinx\Query
$groupLimit integer Groups limit: to return (no more than) N top matches for each group. yii\sphinx\Query
$match string|\yii\db\Expression Text, which should be searched in fulltext mode. yii\sphinx\Query
$options array Per-query options in format: optionName => optionValue They will compose OPTION clause. yii\sphinx\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
$within string WITHIN GROUP ORDER BY clause. yii\sphinx\Query

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__construct() Constructor. yii\sphinx\ActiveQuery
addFacets() Adds additional FACET part of the query. yii\sphinx\Query
addOptions() Adds additional query options. yii\sphinx\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
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
facets() Sets FACET part of the query. yii\sphinx\Query
getConnection() yii\sphinx\Query
getTablesUsedInFrom() yii\sphinx\Query
groupLimit() Sets groups limit: to return (no more than) N top matches for each group. yii\sphinx\Query
init() Initializes the object. yii\sphinx\ActiveQuery
innerJoin() yii\sphinx\Query
join() yii\sphinx\Query
leftJoin() yii\sphinx\Query
match() Sets the fulltext query text. This text will be composed into MATCH operator inside the WHERE clause. yii\sphinx\Query
one() Executes query and returns a single row of result. yii\sphinx\ActiveQuery
options() Sets the query options. yii\sphinx\Query
populate() yii\sphinx\ActiveQuery
rightJoin() yii\sphinx\Query
search() Executes the query and returns the complete search result including e.g. hits, facets. yii\sphinx\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
within() Sets the WITHIN GROUP ORDER BY part of the query. yii\sphinx\Query

Protected Methods

Hide inherited methods

MethodDescriptionDefined 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
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
queryScalar() yii\sphinx\Query

Events

Hide inherited events

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

return yii\sphinx\ActiveRecord|array|null

A single row of query result. Depending on the setting of \yii\sphinx\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

public void populate ( $rows )
$rows
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 \yii\sphinx\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().