Class yii\sphinx\ActiveDataProvider
Inheritance | yii\sphinx\ActiveDataProvider » yii\data\ActiveDataProvider |
---|---|
Available since version | 2.0.4 |
ActiveDataProvider is an enhanced version of \yii\data\ActiveDataProvider specific to the Sphinx.
It allows to fetch not only rows and total rows count, but also a meta information and facet results.
The following is an example of using ActiveDataProvider to provide facet results:
$provider = new ActiveDataProvider([
'query' => Post::find()->facets(['author_id', 'category_id']),
'pagination' => [
'pageSize' => 20,
],
]);
// get the posts in the current page
$posts = $provider->getModels();
// get all facets
$facets = $provider->getFacets();
// get particular facet
$authorFacet = $provider->getFacet('author_id');
In case yii\sphinx\Query::showMeta() is set ActiveDataProvider will fetch total count value from the query meta information, avoiding extra counting query:
$provider = new ActiveDataProvider([
'query' => Post::find()->showMeta(true),
'pagination' => [
'pageSize' => 20,
],
]);
$totalCount = $provider->getTotalCount(); // fetched from meta information
Note: when using 'meta' information results total count will be fetched after pagination limit applying,
which eliminates ability to verify if requested page number actually exist. Data provider disables yii\data\Pagination::validatePage
automatically in this case.
Note: because pagination offset and limit may exceed Sphinx 'max_matches' bounds, data provider will set 'max_matches' option automatically based on those values. However, if yii\sphinx\Query::showMeta() is set, such adjustment is not performed as it will break total count calculation, so you'll have to deal with 'max_matches' bounds on your own.
Public Methods
Method | Description | Defined By |
---|---|---|
getFacet() | Returns results of the specified facet. | yii\sphinx\ActiveDataProvider |
getFacets() | yii\sphinx\ActiveDataProvider | |
getMeta() | yii\sphinx\ActiveDataProvider | |
setFacets() | yii\sphinx\ActiveDataProvider | |
setMeta() | yii\sphinx\ActiveDataProvider |
Protected Methods
Method | Description | Defined By |
---|---|---|
prepareModels() | yii\sphinx\ActiveDataProvider | |
prepareTotalCount() | yii\sphinx\ActiveDataProvider |
Method Details
Returns results of the specified facet.
public array getFacet ( $name ) | ||
$name | string | Facet name |
return | array | Facet results. |
---|---|---|
throws | \yii\base\InvalidCallException | if requested facet does not present in results. |
public array getFacets ( ) | ||
return | array | Query facet results. |
---|
public array getMeta ( ) | ||
return | array | Search query meta info |
---|
protected void prepareModels ( ) |
protected void prepareTotalCount ( ) |
public void setFacets ( $facets ) | ||
$facets | array | Query facet results. |
public void setMeta ( $meta ) | ||
$meta | array | Search query meta info |