Skip to content

Commit 5bc6803

Browse files
authored
Merge pull request #2205 from malarzm/prepare-expressions
Stop type conversion for certain keys in query
2 parents 785ac03 + 53fa39d commit 5bc6803

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,10 @@ private function convertToDatabaseValue(string $fieldName, $value)
10311031
{
10321032
if (is_array($value)) {
10331033
foreach ($value as $k => $v) {
1034+
if ($k === '$exists' || $k === '$type' || $k === '$currentDate') {
1035+
continue;
1036+
}
1037+
10341038
$value[$k] = $this->convertToDatabaseValue($fieldName, $v);
10351039
}
10361040

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Doctrine\ODM\MongoDB\Tests\BaseTest;
1414
use Doctrine\ODM\MongoDB\Types\ClosureToPHP;
1515
use Doctrine\ODM\MongoDB\Types\Type;
16+
use Documents\Article;
1617
use Generator;
1718
use InvalidArgumentException;
1819
use MongoDB\BSON\ObjectId;
@@ -140,6 +141,33 @@ public function getTestPrepareFieldNameData()
140141
];
141142
}
142143

144+
public function testCurrentDateInQuery()
145+
{
146+
$qb = $this->dm->createQueryBuilder(Article::class)
147+
->updateMany()
148+
->field('createdAt')->currentDate();
149+
150+
$this->assertSame(
151+
['$currentDate' => ['createdAt' => ['$type' => 'date']]],
152+
$qb->getQuery()->debug('newObj')
153+
);
154+
}
155+
156+
public function testExistsInQuery()
157+
{
158+
$qb = $this->dm->createQueryBuilder(Article::class)
159+
->field('title')->exists(false)
160+
->field('createdAt')->exists(true);
161+
162+
$this->assertSame(
163+
[
164+
'title' => ['$exists' => false],
165+
'createdAt' => ['$exists' => true],
166+
],
167+
$qb->getQuery()->debug('query')
168+
);
169+
}
170+
143171
/**
144172
* @dataProvider provideHashIdentifiers
145173
*/

0 commit comments

Comments
 (0)