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

Fix prepareQueryOrNewObj with object values #2189

Merged
merged 1 commit into from
May 29, 2020
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
23 changes: 16 additions & 7 deletions lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -1012,10 +1012,7 @@ public function prepareQueryOrNewObj(array $query, bool $isNewObj = false) : arr

$preparedQueryElements = $this->prepareQueryElement((string) $key, $value, null, true, $isNewObj);
foreach ($preparedQueryElements as [$preparedKey, $preparedValue]) {
$preparedValue = Type::convertPHPToDatabaseValue($preparedValue);
if ($this->class->hasField($key)) {
$preparedValue = $this->convertToDatabaseValue($key, $preparedValue);
}
$preparedValue = $this->convertToDatabaseValue((string) $key, $preparedValue);
$preparedQuery[$preparedKey] = $preparedValue;
}
}
Expand All @@ -1024,16 +1021,24 @@ public function prepareQueryOrNewObj(array $query, bool $isNewObj = false) : arr
}

/**
* Converts a single value to its database representation based on the mapping type
* Converts a single value to its database representation based on the mapping type if possible.
*
* @param mixed $value
*
* @return mixed
*/
private function convertToDatabaseValue(string $fieldName, $value)
{
$mapping = $this->class->fieldMappings[$fieldName];
$typeName = $mapping['type'];
$mapping = [];
$typeName = '';
$dbValue = null;
$hasMapping = $this->class->hasField($fieldName);
if ($hasMapping) {
$mapping = $this->class->fieldMappings[$fieldName];
$typeName = $mapping['type'];
} else {
$dbValue = Type::convertPHPToDatabaseValue($value);
}

if (is_array($value)) {
foreach ($value as $k => $v) {
Expand All @@ -1043,6 +1048,10 @@ private function convertToDatabaseValue(string $fieldName, $value)
return $value;
}

if (! $hasMapping) {
return $dbValue;
}

if (! empty($mapping['reference']) || ! empty($mapping['embedded'])) {
return $value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\ODM\MongoDB\Tests\Functional;

use Closure;
use DateTime;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\LockException;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
Expand All @@ -15,6 +16,7 @@
use Generator;
use InvalidArgumentException;
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\UTCDateTime;
use MongoDB\Collection;
use ReflectionProperty;
use function get_class;
Expand Down Expand Up @@ -461,6 +463,43 @@ public function testPrepareQueryOrNewObjWithDBRefReferenceToTargetDocumentWithHa
$this->assertEquals($expected, $documentPersister->prepareQueryOrNewObj($value));
}

/**
* @dataProvider queryProviderForComplexRefWithObjectValue
*/
public function testPrepareQueryOrNewObjWithComplexRefToTargetDocumentFieldWithObjectValue(array $expected, array $query)
{
$class = DocumentPersisterTestDocument::class;
$documentPersister = $this->uow->getDocumentPersister($class);

$this->assertEquals(
$expected,
$documentPersister->prepareQueryOrNewObj($query)
);
}

public static function queryProviderForComplexRefWithObjectValue() : Generator
{
yield 'Direct comparison' => [
'expected' => ['complexRef.date' => new UTCDateTime('1590710400000')],
'query' => ['complexRef.date' => new DateTime('2020-05-29')],
];

yield 'Operator with single value' => [
'expected' => ['complexRef.date' => ['$ne' => new UTCDateTime('1590710400000')]],
'query' => ['complexRef.date' => ['$ne' => new DateTime('2020-05-29')]],
];

yield 'Operator with multiple values' => [
'expected' => ['complexRef.date' => ['$in' => [new UTCDateTime('1590710400000'), new UTCDateTime('1590796800000')]]],
'query' => ['complexRef.date' => ['$in' => [new DateTime('2020-05-29'), new DateTime('2020-05-30')]]],
];

yield 'Nested operator' => [
'expected' => ['complexRef.date' => ['$not' => ['$in' => [new UTCDateTime('1590710400000'), new UTCDateTime('1590796800000')]]]],
'query' => ['complexRef.date' => ['$not' => ['$in' => [new DateTime('2020-05-29'), new DateTime('2020-05-30')]]]],
];
}

public function testPrepareQueryOrNewObjWithEmbeddedReferenceToTargetDocumentWithNormalIdType()
{
$class = DocumentPersisterTestHashIdDocument::class;
Expand Down