Commit 5b0d6f88 by Klimov Paul

`yii\sphinx\ActiveQuery` fixed to be resuable with 'match' condition.

parent 10edf5c9
......@@ -7,6 +7,7 @@ Yii Framework 2 sphinx extension Change Log
- Bug #3668: Escaping of the special characters at 'MATCH' statement added (klimov-paul)
- Bug #4018: AR relation eager loading does not work with db models (klimov-paul)
- Bug #4375: Distributed indexes support provided (klimov-paul)
- Bug #4830: `ActiveQuery` instance reusage ability granted (klimov-paul)
- Enh #3520: Added `unlinkAll()`-method to active record to remove all records of a model relation (NmDimas, samdark, cebe)
- Enh #4048: Added `init` event to `ActiveQuery` classes (qiangxue)
- Enh #4086: changedAttributes of afterSave Event now contain old values (dizews)
......
......@@ -63,17 +63,6 @@ class QueryBuilder extends Object
{
$params = empty($params) ? $query->params : array_merge($params, $query->params);
if ($query->match !== null) {
if ($query->match instanceof Expression) {
$query->andWhere('MATCH(' . $query->match->expression . ')');
$params = array_merge($params, $query->match->params);
} else {
$phName = self::PARAM_PREFIX . count($params);
$params[$phName] = $this->db->escapeMatchValue($query->match);
$query->andWhere('MATCH(' . $phName . ')');
}
}
$from = $query->from;
if ($from === null && $query instanceof ActiveQuery) {
/* @var $modelClass ActiveRecord */
......@@ -84,7 +73,7 @@ class QueryBuilder extends Object
$clauses = [
$this->buildSelect($query->select, $params, $query->distinct, $query->selectOption),
$this->buildFrom($from, $params),
$this->buildWhere($query->from, $query->where, $params),
$this->buildWhere($query->from, $query->where, $params, $query->match),
$this->buildGroupBy($query->groupBy),
$this->buildWithin($query->within),
$this->buildOrderBy($query->orderBy),
......@@ -487,10 +476,28 @@ class QueryBuilder extends Object
* @param string[] $indexes list of index names, which affected by query
* @param string|array $condition
* @param array $params the binding parameters to be populated
* @param string|Expression|null $match
* @return string the WHERE clause built from [[query]].
*/
public function buildWhere($indexes, $condition, &$params)
public function buildWhere($indexes, $condition, &$params, $match = null)
{
if ($match !== null) {
if ($match instanceof Expression) {
$matchWhere = 'MATCH(' . $match->expression . ')';
$params = array_merge($params, $match->params);
} else {
$phName = self::PARAM_PREFIX . count($params);
$params[$phName] = $this->db->escapeMatchValue($match);
$matchWhere = 'MATCH(' . $phName . ')';
}
if ($condition === null) {
$condition = $matchWhere;
} else {
$condition = ['and', $matchWhere, $condition];
}
}
if (empty($condition)) {
return '';
}
......
......@@ -236,6 +236,8 @@ class ActiveRecordTest extends SphinxTestCase
}
/**
* @see https://github.com/yiisoft/yii2/issues/4830
*
* @depends testFind
*/
public function testFindQueryReuse()
......@@ -243,5 +245,9 @@ class ActiveRecordTest extends SphinxTestCase
$result = ArticleIndex::find()->andWhere(['author_id' => 1]);
$this->assertTrue($result->one() instanceof ArticleIndex);
$this->assertTrue($result->one() instanceof ArticleIndex);
$result = ArticleIndex::find()->match('dogs');
$this->assertTrue($result->one() instanceof ArticleIndex);
$this->assertTrue($result->one() instanceof ArticleIndex);
}
}
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