Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow choose fields that doesn't exist #105

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions Filter/ChoiceFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,30 @@ public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
return;
}

$expr = $queryBuilder->expr();

$notExistentFieldKey = array_search('notExistentField', $data['value']);
if (false !== $notExistentFieldKey) {
$expr->addOr($queryBuilder->expr()->field($field)->exists(false));
unset($data['value'][$notExistentFieldKey]);
}

if ($data['type'] == ChoiceType::TYPE_NOT_CONTAINS) {
$queryBuilder->field($field)->notIn($data['value']);
$expr->addOr($queryBuilder->expr()->field($field)->notIn($data['value']));
} else {
$queryBuilder->field($field)->in($data['value']);
$expr->addOr($queryBuilder->expr()->field($field)->in($data['value']));
}

$queryBuilder->addAnd($expr);
$this->active = true;
} else {
if ($data['value'] === '' || $data['value'] === null || $data['value'] === false || $data['value'] === 'all') {
return;
}

if ($data['type'] == ChoiceType::TYPE_NOT_CONTAINS) {
if ('notExistentField' == $data['value']) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use === instead of == everywhere please (type are not checked everywhere in the existing code base but this will be fixed).

$queryBuilder->field($field)->exists(false);
} elseif ($data['type'] == ChoiceType::TYPE_NOT_CONTAINS) {
$queryBuilder->field($field)->notEqual($data['value']);
} else {
$queryBuilder->field($field)->equals($data['value']);
Expand Down
23 changes: 17 additions & 6 deletions Tests/Filter/FilterWithQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,24 @@ class FilterWithQueryBuilderTest extends \PHPUnit_Framework_TestCase
public function setUp()
{
$this->queryBuilder = $this->getMockBuilder('Doctrine\ODM\MongoDB\Query\Builder')
->disableOriginalConstructor()
->getMock();
->disableOriginalConstructor()
->getMock();
$this->queryBuilder
->expects($this->any())
->method('field')
->will($this->returnSelf())
;
->expects($this->any())
->method('field')
->will($this->returnSelf());
$this->queryBuilder
->expects($this->any())
->method('expr')
->will($this->returnSelf());
$this->queryBuilder
->expects($this->any())
->method('addOr')
->will($this->returnSelf());
$this->queryBuilder
->expects($this->any())
->method('addAnd')
->will($this->returnSelf());
}

protected function getQueryBuilder()
Expand Down