You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
2.4 KiB
109 lines
2.4 KiB
11 years ago
|
Sphinx Extension for Yii 2
|
||
|
==========================
|
||
11 years ago
|
|
||
11 years ago
|
This extension adds [Sphinx](http://sphinxsearch.com/docs) full text search engine extension for the Yii 2 framework.
|
||
11 years ago
|
|
||
|
|
||
|
Installation
|
||
|
------------
|
||
|
|
||
|
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
|
||
|
|
||
|
Either run
|
||
11 years ago
|
|
||
11 years ago
|
```
|
||
|
php composer.phar require yiisoft/yii2-sphinx "*"
|
||
|
```
|
||
|
|
||
|
or add
|
||
11 years ago
|
|
||
|
```json
|
||
11 years ago
|
"yiisoft/yii2-sphinx": "*"
|
||
|
```
|
||
|
|
||
11 years ago
|
to the require section of your composer.json.
|
||
11 years ago
|
|
||
|
|
||
|
Usage & Documentation
|
||
|
---------------------
|
||
|
|
||
11 years ago
|
This extension interacts with Sphinx search daemon using MySQL protocol and [SphinxQL](http://sphinxsearch.com/docs/current.html#sphinxql) query language.
|
||
11 years ago
|
In order to setup Sphinx "searchd" to support MySQL protocol following configuration should be added:
|
||
11 years ago
|
|
||
11 years ago
|
```
|
||
|
searchd
|
||
|
{
|
||
|
listen = localhost:9306:mysql41
|
||
|
...
|
||
|
}
|
||
|
```
|
||
11 years ago
|
|
||
11 years ago
|
This extension supports all Sphinx features including [Runtime Indexes](http://sphinxsearch.com/docs/current.html#rt-indexes).
|
||
|
Since this extension uses MySQL protocol to access Sphinx, it shares base approach and much code from the
|
||
11 years ago
|
regular "yii\db" package.
|
||
|
|
||
|
To use this extension, simply add the following code in your application configuration:
|
||
|
|
||
|
```php
|
||
|
return [
|
||
|
//....
|
||
|
'components' => [
|
||
|
'sphinx' => [
|
||
|
'class' => 'yii\sphinx\Connection',
|
||
|
'dsn' => 'mysql:host=127.0.0.1;port=9306;',
|
||
|
'username' => '',
|
||
|
'password' => '',
|
||
|
],
|
||
|
],
|
||
|
];
|
||
11 years ago
|
```
|
||
|
|
||
|
This extension provides ActiveRecord solution similar ot the [[\yii\db\ActiveRecord]].
|
||
|
To declare an ActiveRecord class you need to extend [[\yii\sphinx\ActiveRecord]] and
|
||
|
implement the `indexName` method:
|
||
|
|
||
|
```php
|
||
|
use yii\sphinx\ActiveRecord;
|
||
|
|
||
|
class Article extends ActiveRecord
|
||
|
{
|
||
|
/**
|
||
|
* @return string the name of the index associated with this ActiveRecord class.
|
||
|
*/
|
||
|
public static function indexName()
|
||
|
{
|
||
|
return 'idx_article';
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
You can use [[\yii\data\ActiveDataProvider]] with the [[\yii\sphinx\Query]] and [[\yii\sphinx\ActiveQuery]]:
|
||
|
|
||
|
```php
|
||
|
use yii\data\ActiveDataProvider;
|
||
|
use yii\sphinx\Query;
|
||
|
|
||
|
$query = new Query;
|
||
|
$query->from('yii2_test_article_index')->match('development');
|
||
|
$provider = new ActiveDataProvider([
|
||
|
'query' => $query,
|
||
|
'pagination' => [
|
||
|
'pageSize' => 10,
|
||
|
]
|
||
|
]);
|
||
|
$models = $provider->getModels();
|
||
|
```
|
||
|
|
||
|
```php
|
||
|
use yii\data\ActiveDataProvider;
|
||
|
use app\models\Article;
|
||
|
|
||
|
$provider = new ActiveDataProvider([
|
||
|
'query' => Article::find(),
|
||
|
'pagination' => [
|
||
|
'pageSize' => 10,
|
||
|
]
|
||
|
]);
|
||
|
$models = $provider->getModels();
|
||
11 years ago
|
```
|