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 mixed value in $not operator #2307

Merged
merged 1 commit into from
May 20, 2021
Merged
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
6 changes: 3 additions & 3 deletions lib/Doctrine/ODM/MongoDB/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1028,11 +1028,11 @@ public function nearSphere($x, $y = null): self
* @see Expr::not()
* @see https://docs.mongodb.com/manual/reference/operator/not/
*
* @param array|Expr $expression
* @param array|Expr|mixed $valueOrExpression
*/
public function not($expression): self
public function not($valueOrExpression): self
{
$this->expr->not($expression);
$this->expr->not($valueOrExpression);

return $this;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Doctrine/ODM/MongoDB/Query/Expr.php
Original file line number Diff line number Diff line change
Expand Up @@ -861,11 +861,11 @@ public function nearSphere($x, $y = null): self
* @see Builder::not()
* @see https://docs.mongodb.com/manual/reference/operator/not/
*
* @param array|Expr $expression
* @param array|Expr|mixed $valueOrExpression
*/
public function not($expression): self
public function not($valueOrExpression): self
{
return $this->operator('$not', $expression);
return $this->operator('$not', $valueOrExpression);
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use InvalidArgumentException;
use IteratorAggregate;
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\Regex;
use MongoDB\BSON\UTCDateTime;

use function array_values;
Expand Down Expand Up @@ -97,6 +98,27 @@ public function testAddNot()
$this->assertNotNull($user);
}

public function testNotAllowsRegex()
{
$user = new User();
$user->setUsername('boo');

$this->dm->persist($user);
$this->dm->flush();

$qb = $this->dm->createQueryBuilder(User::class);
$qb->field('username')->not(new Regex('Boo', 'i'));
$query = $qb->getQuery();
$user = $query->getSingleResult();
$this->assertNull($user);

$qb = $this->dm->createQueryBuilder(User::class);
$qb->field('username')->not(new Regex('Boo'));
$query = $qb->getQuery();
$user = $query->getSingleResult();
$this->assertNotNull($user);
}

public function testDistinct()
{
$user = new User();
Expand Down
14 changes: 14 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use InvalidArgumentException;
use IteratorAggregate;
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\Regex;
use MongoDB\Driver\ReadPreference;
use ReflectionProperty;

Expand Down Expand Up @@ -299,6 +300,19 @@ public function testAddNot()
$this->assertEquals($expected, $qb->getQueryArray());
}

public function testNotAllowsRegex()
{
$qb = $this->getTestQueryBuilder();
$qb->field('username')->not(new Regex('Boo', 'i'));

$expected = [
'username' => [
'$not' => new Regex('Boo', 'i'),
],
];
$this->assertEquals($expected, $qb->getQueryArray());
}

public function testFindQuery()
{
$qb = $this->getTestQueryBuilder()
Expand Down