Skip to content

Commit

Permalink
Allow using enums directly in queries
Browse files Browse the repository at this point in the history
  • Loading branch information
IonBazan committed May 11, 2022
1 parent 0dba487 commit 237766a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\ODM\MongoDB\Persisters;

use BackedEnum;
use BadMethodCallException;
use Doctrine\ODM\MongoDB\Aggregation\Stage\Sort;
use Doctrine\ODM\MongoDB\DocumentManager;
Expand Down Expand Up @@ -1128,6 +1129,10 @@ private function convertToDatabaseValue(string $fieldName, $value)
}

if (! $this->class->hasField($fieldName)) {
if ($value instanceof BackedEnum) {
$value = $value->value;
}

return Type::convertPHPToDatabaseValue($value);
}

Expand All @@ -1144,6 +1149,10 @@ private function convertToDatabaseValue(string $fieldName, $value)
);
}

if ($value instanceof BackedEnum && isset($mapping['enumType'])) {
$value = $value->value;
}

if (in_array($typeName, ['collection', 'hash'])) {
return $value;
}
Expand Down
48 changes: 48 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Documents81\Card;
use Documents81\Suit;
use Error;
use MongoDB\BSON\ObjectId;
use ValueError;

Expand Down Expand Up @@ -49,6 +50,53 @@ public function testLoadingInvalidBackingValueThrowsError(): void
$this->dm->getRepository(Card::class)->findOneBy([]);
}

public function testQueryWithMappedField(): void
{
$qb = $this->dm->createQueryBuilder(Card::class)
->field('suit')->equals(Suit::Spades)
->field('nullableSuit')->in([Suit::Hearts, Suit::Diamonds]);

$this->assertSame([
'suit' => 'S',
'nullableSuit' => [
'$in' => ['H', 'D'],
],
], $qb->getQuery()->debug('query'));
}

public function testQueryWithMappedFieldAndEnumValue(): void
{
$qb = $this->dm->createQueryBuilder(Card::class)
->field('suit')->equals('S') // Suit::Spades->value
->field('nullableSuit')->in(['H', 'D']);

$this->assertSame([
'suit' => 'S',
'nullableSuit' => [
'$in' => ['H', 'D'],
],
], $qb->getQuery()->debug('query'));
}

public function testQueryWithNotMappedField(): void
{
$qb = $this->dm->createQueryBuilder(Card::class)
->field('nonExisting')->equals(Suit::Clubs)
->field('nonExistingArray')->equals([Suit::Clubs, Suit::Hearts]);

$this->assertSame(['nonExisting' => 'C', 'nonExistingArray' => ['C', 'H']], $qb->getQuery()->debug('query'));
}

public function testQueryWithMappedNonEnumFieldIsPassedToTypeDirectly(): void
{
$this->expectException(Error::class);
$this->expectExceptionMessage(sprintf('Object of class %s could not be converted to string', Suit::class));

$qb = $this->dm->createQueryBuilder(Card::class)->field('_id')->equals(Suit::Clubs);

$this->assertSame(['_id' => 'C'], $qb->getQuery()->debug('query'));
}

protected function createMetadataDriverImpl(): MappingDriver
{
return AttributeDriver::create(__DIR__ . '/../../../Documents');
Expand Down

0 comments on commit 237766a

Please sign in to comment.