Skip to content

Commit

Permalink
Remove using mode for multiple values.
Browse files Browse the repository at this point in the history
This never worked, as a single field can only ever hold one of the
supplied values, hence using `AND` would never match anything.

`mode` is now being used for defining the mode for multiple fields
instead.
  • Loading branch information
ndm2 committed Apr 25, 2017
1 parent a770d39 commit 62c6a30
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 61 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ The following options are supported by all filters except `Callback` and `Finder
multiple values. If disabled, and multiple values are being passed, the filter
will fall back to using the default value defined by the `defaultValue` option.

- `mode` (`string`, defaults to `OR`) The conditional mode to use when searching for
multiple values. Valid values are `OR` and `AND`.
- `mode` (`string`, defaults to `OR`) The conditional mode to use when matching
against multiple fields. Valid values are `OR` and `AND`.

#### `Finder`

Expand Down
38 changes: 5 additions & 33 deletions src/Model/Filter/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace Search\Model\Filter;

use Cake\Database\Expression\QueryExpression;
use Search\Manager;

class Value extends Base
{
Expand All @@ -13,28 +12,9 @@ class Value extends Base
* @var array
*/
protected $_defaultConfig = [
'mode' => null,
'fieldMode' => 'OR',
'valueMode' => 'OR',
'mode' => 'OR'
];

/**
* {@inheritDoc}
*
* @param string $name Name.
* @param \Search\Manager $manager Manager.
* @param array $config Config.
*/
public function __construct($name, Manager $manager, array $config = [])
{
parent::__construct($name, $manager, $config);

$mode = $this->config('mode');
if ($mode !== null) {
$this->config('valueMode', $mode);
}
}

/**
* Process a value condition ($x == $y).
*
Expand All @@ -58,27 +38,19 @@ public function process()
return;
}

$valueMode = strtoupper($this->config('valueMode'));

$expressions = [];
foreach ($this->fields() as $field) {
$expressions[] = function (QueryExpression $e) use ($field, $value, $isMultiValue, $valueMode) {
if ($valueMode === 'OR' &&
$isMultiValue
) {
$expressions[] = function (QueryExpression $e) use ($field, $value, $isMultiValue) {
if ($isMultiValue) {
return $e->in($field, $value);
}

foreach ((array)$value as $val) {
$e->eq($field, $val);
}

return $e;
return $e->eq($field, $value);
};
}

if (!empty($expressions)) {
$this->query()->andWhere([$this->config('fieldMode') => $expressions]);
$this->query()->andWhere([$this->config('mode') => $expressions]);
}
}
}
37 changes: 11 additions & 26 deletions tests/TestCase/Model/Filter/ValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,6 @@ class ValueTest extends TestCase
'plugin.Search.Articles'
];

/**
* @return void
*/
public function testDeprecatedModeOption()
{
$articles = TableRegistry::get('Articles');
$manager = new Manager($articles);
$filter = new Value('title', $manager, ['mode' => 'modeValue']);

$this->assertEquals('modeValue', $filter->config('mode'));
$this->assertEquals('modeValue', $filter->config('valueMode'));
$this->assertEquals('OR', $filter->config('fieldMode'));
}

/**
* @return void
*/
Expand Down Expand Up @@ -82,11 +68,11 @@ public function testProcess()
/**
* @return void
*/
public function testProcessSingleValueWithAndValueMode()
public function testProcessSingleValueWithAndMode()
{
$articles = TableRegistry::get('Articles');
$manager = new Manager($articles);
$filter = new Value('title', $manager, ['valueMode' => 'and']);
$filter = new Value('title', $manager, ['mode' => 'and']);
$filter->args(['title' => 'foo']);
$filter->query($articles->find());
$filter->process();
Expand All @@ -104,13 +90,12 @@ public function testProcessSingleValueWithAndValueMode()
/**
* @return void
*/
public function testProcessSingleValueAndMultiFieldWithAndValueMode()
public function testProcessSingleValueAndMultiField()
{
$articles = TableRegistry::get('Articles');
$manager = new Manager($articles);
$filter = new Value('title', $manager, [
'field' => ['title', 'other'],
'valueMode' => 'and'
'field' => ['title', 'other']
]);
$filter->args(['title' => 'foo']);
$filter->query($articles->find());
Expand All @@ -129,13 +114,13 @@ public function testProcessSingleValueAndMultiFieldWithAndValueMode()
/**
* @return void
*/
public function testProcessSingleValueAndMultiFieldWithAndFieldMode()
public function testProcessSingleValueAndMultiFieldWithAndMode()
{
$articles = TableRegistry::get('Articles');
$manager = new Manager($articles);
$filter = new Value('title', $manager, [
'field' => ['title', 'other'],
'fieldMode' => 'and'
'mode' => 'and'
]);
$filter->args(['title' => 'foo']);
$filter->query($articles->find());
Expand Down Expand Up @@ -176,20 +161,20 @@ public function testProcessMultiValue()
/**
* @return void
*/
public function testProcessMultiValueWithAndValueMode()
public function testProcessMultiValueWithAndMode()
{
$articles = TableRegistry::get('Articles');
$manager = new Manager($articles);
$filter = new Value('title', $manager, [
'multiValue' => true,
'valueMode' => 'and'
'mode' => 'and'
]);
$filter->args(['title' => ['foo', 'bar']]);
$filter->query($articles->find());
$filter->process();

$this->assertRegExp(
'/WHERE \(Articles\.title = :c0 AND Articles\.title = :c1\)$/',
'/WHERE Articles\.title IN \(:c0,:c1\)$/',
$filter->query()->sql()
);
$this->assertEquals(
Expand Down Expand Up @@ -227,14 +212,14 @@ public function testProcessMultiValueAndMultiField()
/**
* @return void
*/
public function testProcessMultiValueAndMultiFieldWithAndFieldMode()
public function testProcessMultiValueAndMultiFieldWithAndMode()
{
$articles = TableRegistry::get('Articles');
$manager = new Manager($articles);
$filter = new Value('title', $manager, [
'multiValue' => true,
'field' => ['title', 'other'],
'fieldMode' => 'and'
'mode' => 'and'
]);
$filter->args(['title' => ['foo', 'bar']]);
$filter->query($articles->find());
Expand Down

0 comments on commit 62c6a30

Please sign in to comment.