Skip to content

Commit 9066f03

Browse files
authored
Merge pull request #2447 from IonBazan/bugfix/enum-queries
Allow using enums directly in queries
2 parents 21d650f + 237766a commit 9066f03

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Doctrine\ODM\MongoDB\Persisters;
66

7+
use BackedEnum;
78
use BadMethodCallException;
89
use Doctrine\ODM\MongoDB\Aggregation\Stage\Sort;
910
use Doctrine\ODM\MongoDB\DocumentManager;
@@ -1128,6 +1129,10 @@ private function convertToDatabaseValue(string $fieldName, $value)
11281129
}
11291130

11301131
if (! $this->class->hasField($fieldName)) {
1132+
if ($value instanceof BackedEnum) {
1133+
$value = $value->value;
1134+
}
1135+
11311136
return Type::convertPHPToDatabaseValue($value);
11321137
}
11331138

@@ -1144,6 +1149,10 @@ private function convertToDatabaseValue(string $fieldName, $value)
11441149
);
11451150
}
11461151

1152+
if ($value instanceof BackedEnum && isset($mapping['enumType'])) {
1153+
$value = $value->value;
1154+
}
1155+
11471156
if (in_array($typeName, ['collection', 'hash'])) {
11481157
return $value;
11491158
}

tests/Doctrine/ODM/MongoDB/Tests/Functional/EnumTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
1010
use Documents81\Card;
1111
use Documents81\Suit;
12+
use Error;
1213
use MongoDB\BSON\ObjectId;
1314
use ValueError;
1415

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

53+
public function testQueryWithMappedField(): void
54+
{
55+
$qb = $this->dm->createQueryBuilder(Card::class)
56+
->field('suit')->equals(Suit::Spades)
57+
->field('nullableSuit')->in([Suit::Hearts, Suit::Diamonds]);
58+
59+
$this->assertSame([
60+
'suit' => 'S',
61+
'nullableSuit' => [
62+
'$in' => ['H', 'D'],
63+
],
64+
], $qb->getQuery()->debug('query'));
65+
}
66+
67+
public function testQueryWithMappedFieldAndEnumValue(): void
68+
{
69+
$qb = $this->dm->createQueryBuilder(Card::class)
70+
->field('suit')->equals('S') // Suit::Spades->value
71+
->field('nullableSuit')->in(['H', 'D']);
72+
73+
$this->assertSame([
74+
'suit' => 'S',
75+
'nullableSuit' => [
76+
'$in' => ['H', 'D'],
77+
],
78+
], $qb->getQuery()->debug('query'));
79+
}
80+
81+
public function testQueryWithNotMappedField(): void
82+
{
83+
$qb = $this->dm->createQueryBuilder(Card::class)
84+
->field('nonExisting')->equals(Suit::Clubs)
85+
->field('nonExistingArray')->equals([Suit::Clubs, Suit::Hearts]);
86+
87+
$this->assertSame(['nonExisting' => 'C', 'nonExistingArray' => ['C', 'H']], $qb->getQuery()->debug('query'));
88+
}
89+
90+
public function testQueryWithMappedNonEnumFieldIsPassedToTypeDirectly(): void
91+
{
92+
$this->expectException(Error::class);
93+
$this->expectExceptionMessage(sprintf('Object of class %s could not be converted to string', Suit::class));
94+
95+
$qb = $this->dm->createQueryBuilder(Card::class)->field('_id')->equals(Suit::Clubs);
96+
97+
$this->assertSame(['_id' => 'C'], $qb->getQuery()->debug('query'));
98+
}
99+
52100
protected function createMetadataDriverImpl(): MappingDriver
53101
{
54102
return AttributeDriver::create(__DIR__ . '/../../../Documents');

0 commit comments

Comments
 (0)