Browse Source

"snippetByModel" option added to Sphinx Active Query.

tags/2.0.0-beta
Klimov Paul 11 years ago
parent
commit
e1065ef1fc
  1. 43
      extensions/sphinx/ActiveQuery.php
  2. 15
      extensions/sphinx/Query.php
  3. 5
      tests/unit/data/sphinx/ar/ArticleIndex.php
  4. 13
      tests/unit/extensions/sphinx/ActiveRelationTest.php

43
extensions/sphinx/ActiveQuery.php

@ -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
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
$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
{ {
/** @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
} }
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;
}
} }

15
extensions/sphinx/Query.php

@ -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
*/ */
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
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();
} }
} }

5
tests/unit/data/sphinx/ar/ArticleIndex.php

@ -28,4 +28,9 @@ class ArticleIndex extends ActiveRecord
]; ];
return new ActiveRelation($config); return new ActiveRelation($config);
} }
public function getSnippetSource()
{
return $this->source->content;
}
} }

13
tests/unit/extensions/sphinx/ActiveRelationTest.php

@ -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));
}
} }
Loading…
Cancel
Save