From e1065ef1fc17599a99f32c428a7ab54fee020d7d Mon Sep 17 00:00:00 2001 From: Klimov Paul Date: Sun, 17 Nov 2013 17:02:47 +0200 Subject: [PATCH] "snippetByModel" option added to Sphinx Active Query. --- extensions/sphinx/ActiveQuery.php | 43 ++++++++++++++++++++-- extensions/sphinx/Query.php | 15 +++++--- tests/unit/data/sphinx/ar/ArticleIndex.php | 5 +++ .../unit/extensions/sphinx/ActiveRelationTest.php | 13 +++++++ 4 files changed, 68 insertions(+), 8 deletions(-) diff --git a/extensions/sphinx/ActiveQuery.php b/extensions/sphinx/ActiveQuery.php index 91ed96f..aaaf419 100644 --- a/extensions/sphinx/ActiveQuery.php +++ b/extensions/sphinx/ActiveQuery.php @@ -27,6 +27,19 @@ class ActiveQuery extends Query implements ActiveQueryInterface public $sql; /** + * Sets the [[snippetCallback]] to [[fetchSnippetSourceFromModels]], which allows to + * fetch the snippet source strings from the Active Record models, using method + * [[ActiveRecord::getSnippetSource()]]. + * Warning: this option should NOT be used with [[asArray]] at the same time! + * @return static the query object itself + */ + public function snippetByModel() + { + $this->snippetCallback(array($this, 'fetchSnippetSourceFromModels')); + return $this; + } + + /** * Executes query and returns all results as an array. * @param Connection $db the DB connection used to create the DB command. * If null, the DB connection returned by [[modelClass]] will be used. @@ -41,6 +54,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface if (!empty($this->with)) { $this->findWith($this->with, $models); } + $models = $this->fillUpSnippets($models); return $models; } else { return []; @@ -72,6 +86,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface $this->findWith($this->with, $models); $model = $models[0]; } + list ($model) = $this->fillUpSnippets([$model]); return $model; } else { return null; @@ -88,9 +103,8 @@ class ActiveQuery extends Query implements ActiveQueryInterface { /** @var $modelClass ActiveRecord */ $modelClass = $this->modelClass; - if ($db === null) { - $db = $modelClass::getDb(); - } + $this->setConnection($db); + $db = $this->getConnection(); $params = $this->params; if ($this->sql === null) { @@ -105,4 +119,27 @@ class ActiveQuery extends Query implements ActiveQueryInterface } return $db->createCommand($this->sql, $params); } + + /** + * @inheritdoc + */ + protected function defaultConnection() + { + $modelClass = $this->modelClass; + return $modelClass::getDb(); + } + + /** + * Fetches the source for the snippets using [[ActiveRecord::getSnippetSource()]] method. + * @param ActiveRecord[] $models raw query result rows. + * @return array snippet source strings + */ + protected function fetchSnippetSourceFromModels($models) + { + $result = []; + foreach ($models as $model) { + $result[] = $model->getSnippetSource(); + } + return $result; + } } \ No newline at end of file diff --git a/extensions/sphinx/Query.php b/extensions/sphinx/Query.php index 1d66887..fa67ad2 100644 --- a/extensions/sphinx/Query.php +++ b/extensions/sphinx/Query.php @@ -9,6 +9,7 @@ namespace yii\sphinx; use Yii; use yii\base\Component; +use yii\base\InvalidCallException; use yii\db\Expression; /** @@ -235,11 +236,11 @@ class Query extends Component */ public function one($db = null) { - $result = $this->createCommand($db)->queryOne(); - if ($result) { - list ($result) = $this->fillUpSnippets([$result]); + $row = $this->createCommand($db)->queryOne(); + if ($row !== false) { + list ($row) = $this->fillUpSnippets([$row]); } - return $result; + return $row; } /** @@ -797,8 +798,12 @@ class Query extends Component protected function callSnippets(array $source) { $connection = $this->getConnection(); + $match = $this->match; + if ($match === null) { + throw new InvalidCallException('Unable to call snippets: "' . $this->className() . '::match" should be specified.'); + } return $connection->createCommand() - ->callSnippets($this->from[0], $source, $this->match, $this->snippetOptions) + ->callSnippets($this->from[0], $source, $match, $this->snippetOptions) ->queryColumn(); } } \ No newline at end of file diff --git a/tests/unit/data/sphinx/ar/ArticleIndex.php b/tests/unit/data/sphinx/ar/ArticleIndex.php index f7f0ef7..7c8c079 100644 --- a/tests/unit/data/sphinx/ar/ArticleIndex.php +++ b/tests/unit/data/sphinx/ar/ArticleIndex.php @@ -28,4 +28,9 @@ class ArticleIndex extends ActiveRecord ]; return new ActiveRelation($config); } + + public function getSnippetSource() + { + return $this->source->content; + } } \ No newline at end of file diff --git a/tests/unit/extensions/sphinx/ActiveRelationTest.php b/tests/unit/extensions/sphinx/ActiveRelationTest.php index b304086..343a58b 100644 --- a/tests/unit/extensions/sphinx/ActiveRelationTest.php +++ b/tests/unit/extensions/sphinx/ActiveRelationTest.php @@ -39,4 +39,17 @@ class ActiveRelationTest extends SphinxTestCase $this->assertTrue($articles[0]->source instanceof ArticleDb); $this->assertTrue($articles[1]->source instanceof ArticleDb); } + + /** + * @depends testFindEager + */ + public function testFindWithSnippets() + { + $articles = ArticleIndex::find() + ->match('about') + ->with('source') + ->snippetByModel() + ->all(); + $this->assertEquals(2, count($articles)); + } } \ No newline at end of file