Commit e1065ef1 by Klimov Paul

"snippetByModel" option added to Sphinx Active Query.

parent 386b58b2
...@@ -27,6 +27,19 @@ class ActiveQuery extends Query implements ActiveQueryInterface ...@@ -27,6 +27,19 @@ class ActiveQuery extends Query implements ActiveQueryInterface
public $sql; 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. * Executes query and returns all results as an array.
* @param Connection $db the DB connection used to create the DB command. * @param Connection $db the DB connection used to create the DB command.
* If null, the DB connection returned by [[modelClass]] will be used. * If null, the DB connection returned by [[modelClass]] will be used.
...@@ -41,6 +54,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface ...@@ -41,6 +54,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface
if (!empty($this->with)) { if (!empty($this->with)) {
$this->findWith($this->with, $models); $this->findWith($this->with, $models);
} }
$models = $this->fillUpSnippets($models);
return $models; return $models;
} else { } else {
return []; return [];
...@@ -72,6 +86,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface ...@@ -72,6 +86,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface
$this->findWith($this->with, $models); $this->findWith($this->with, $models);
$model = $models[0]; $model = $models[0];
} }
list ($model) = $this->fillUpSnippets([$model]);
return $model; return $model;
} else { } else {
return null; return null;
...@@ -88,9 +103,8 @@ class ActiveQuery extends Query implements ActiveQueryInterface ...@@ -88,9 +103,8 @@ class ActiveQuery extends Query implements ActiveQueryInterface
{ {
/** @var $modelClass ActiveRecord */ /** @var $modelClass ActiveRecord */
$modelClass = $this->modelClass; $modelClass = $this->modelClass;
if ($db === null) { $this->setConnection($db);
$db = $modelClass::getDb(); $db = $this->getConnection();
}
$params = $this->params; $params = $this->params;
if ($this->sql === null) { if ($this->sql === null) {
...@@ -105,4 +119,27 @@ class ActiveQuery extends Query implements ActiveQueryInterface ...@@ -105,4 +119,27 @@ class ActiveQuery extends Query implements ActiveQueryInterface
} }
return $db->createCommand($this->sql, $params); 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
...@@ -9,6 +9,7 @@ namespace yii\sphinx; ...@@ -9,6 +9,7 @@ namespace yii\sphinx;
use Yii; use Yii;
use yii\base\Component; use yii\base\Component;
use yii\base\InvalidCallException;
use yii\db\Expression; use yii\db\Expression;
/** /**
...@@ -235,11 +236,11 @@ class Query extends Component ...@@ -235,11 +236,11 @@ class Query extends Component
*/ */
public function one($db = null) public function one($db = null)
{ {
$result = $this->createCommand($db)->queryOne(); $row = $this->createCommand($db)->queryOne();
if ($result) { if ($row !== false) {
list ($result) = $this->fillUpSnippets([$result]); list ($row) = $this->fillUpSnippets([$row]);
} }
return $result; return $row;
} }
/** /**
...@@ -797,8 +798,12 @@ class Query extends Component ...@@ -797,8 +798,12 @@ class Query extends Component
protected function callSnippets(array $source) protected function callSnippets(array $source)
{ {
$connection = $this->getConnection(); $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() return $connection->createCommand()
->callSnippets($this->from[0], $source, $this->match, $this->snippetOptions) ->callSnippets($this->from[0], $source, $match, $this->snippetOptions)
->queryColumn(); ->queryColumn();
} }
} }
\ No newline at end of file
...@@ -28,4 +28,9 @@ class ArticleIndex extends ActiveRecord ...@@ -28,4 +28,9 @@ class ArticleIndex extends ActiveRecord
]; ];
return new ActiveRelation($config); return new ActiveRelation($config);
} }
public function getSnippetSource()
{
return $this->source->content;
}
} }
\ No newline at end of file
...@@ -39,4 +39,17 @@ class ActiveRelationTest extends SphinxTestCase ...@@ -39,4 +39,17 @@ class ActiveRelationTest extends SphinxTestCase
$this->assertTrue($articles[0]->source instanceof ArticleDb); $this->assertTrue($articles[0]->source instanceof ArticleDb);
$this->assertTrue($articles[1]->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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment