README.md 2.4 KB
Newer Older
Qiang Xue committed
1 2
Sphinx Extension for Yii 2
==========================
3

Qiang Xue committed
4
This extension adds [Sphinx](http://sphinxsearch.com/docs) full text search engine extension for the Yii 2 framework.
5 6 7 8 9 10 11 12


Installation
------------

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run
Qiang Xue committed
13

14
```
15
php composer.phar require --prefer-dist yiisoft/yii2-sphinx "*"
16 17 18
```

or add
Qiang Xue committed
19 20

```json
21 22 23
"yiisoft/yii2-sphinx": "*"
```

Qiang Xue committed
24
to the require section of your composer.json.
25 26 27 28 29


Usage & Documentation
---------------------

Qiang Xue committed
30
This extension interacts with Sphinx search daemon using MySQL protocol and [SphinxQL](http://sphinxsearch.com/docs/current.html#sphinxql) query language.
31
In order to setup Sphinx "searchd" to support MySQL protocol following configuration should be added:
Qiang Xue committed
32

33 34 35 36 37 38 39
```
searchd
{
	listen = localhost:9306:mysql41
	...
}
```
40

41 42
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
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
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' => '',
		],
	],
];
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
```

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();
Qiang Xue committed
108
```