From 400b5310cae8fd6431570bc1083e5cd839ac7b5c Mon Sep 17 00:00:00 2001 From: Paul Klimov Date: Fri, 22 Nov 2013 14:06:32 +0200 Subject: [PATCH] Docs for Sphinx extension updated. --- extensions/sphinx/ActiveRecord.php | 2 +- extensions/sphinx/README.md | 49 ++++++++++++++++++++++ .../extensions/sphinx/ActiveDataProviderTest.php | 24 +++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/extensions/sphinx/ActiveRecord.php b/extensions/sphinx/ActiveRecord.php index e62620c..a59c065 100644 --- a/extensions/sphinx/ActiveRecord.php +++ b/extensions/sphinx/ActiveRecord.php @@ -35,7 +35,7 @@ use Yii; * @author Paul Klimov * @since 2.0 */ -class ActiveRecord extends Model +abstract class ActiveRecord extends Model { /** * @event Event an event that is triggered when the record is initialized via [[init()]]. diff --git a/extensions/sphinx/README.md b/extensions/sphinx/README.md index 35c400a..ae7c285 100644 --- a/extensions/sphinx/README.md +++ b/extensions/sphinx/README.md @@ -66,4 +66,53 @@ return [ ], ], ]; +``` + +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(); ``` \ No newline at end of file diff --git a/tests/unit/extensions/sphinx/ActiveDataProviderTest.php b/tests/unit/extensions/sphinx/ActiveDataProviderTest.php index 17a0970..6a81900 100644 --- a/tests/unit/extensions/sphinx/ActiveDataProviderTest.php +++ b/tests/unit/extensions/sphinx/ActiveDataProviderTest.php @@ -3,6 +3,7 @@ namespace yiiunit\extensions\sphinx; use yii\data\ActiveDataProvider; +use yii\sphinx\Query; use yiiunit\data\sphinx\ar\ActiveRecord; use yiiunit\data\sphinx\ar\ArticleIndex; @@ -19,6 +20,29 @@ class ActiveDataProviderTest extends SphinxTestCase // Tests : + public function testQuery() + { + $query = new Query; + $query->from('yii2_test_article_index'); + + $provider = new ActiveDataProvider([ + 'query' => $query, + 'db' => $this->getConnection(), + ]); + $models = $provider->getModels(); + $this->assertEquals(2, count($models)); + + $provider = new ActiveDataProvider([ + 'query' => $query, + 'db' => $this->getConnection(), + 'pagination' => [ + 'pageSize' => 1, + ] + ]); + $models = $provider->getModels(); + $this->assertEquals(1, count($models)); + } + public function testActiveQuery() { $provider = new ActiveDataProvider([