Skip to content

Commit 7620b56

Browse files
committed
Fix preparation of references in queries
1 parent 1859767 commit 7620b56

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,30 @@ private function prepareQueryElement(string $originalFieldName, $value = null, ?
12591259
return [[$fieldName, $prepareValue ? $this->convertToDatabaseValue($fieldNameParts[0], $value) : $value]];
12601260
}
12611261

1262+
// Don't recurse for references in queries. Instead, prepare them directly
1263+
if (! $inNewObj && ! empty($mapping['reference'])) {
1264+
// First part is the name of the reference
1265+
// Second part is either a positional operator, index/key, or the name of a field
1266+
// Third part (if any) is the name of a field
1267+
// That means, we can implode all field parts except the first as the next field name
1268+
if ($fieldNameParts[1] === '$') {
1269+
assert($partCount >= 3);
1270+
$objectProperty = $fieldNameParts[2];
1271+
$referencePrefix = $fieldNamePrefix . $mapping['name'] . '.$';
1272+
} else {
1273+
$objectProperty = $fieldNameParts[1];
1274+
$referencePrefix = $fieldNamePrefix . $mapping['name'];
1275+
}
1276+
1277+
if ($targetClass->hasField($objectProperty) && $targetClass->isIdentifier($objectProperty)) {
1278+
$fieldName = ClassMetadata::getReferenceFieldName($mapping['storeAs'], $referencePrefix);
1279+
1280+
return [[$fieldName, $prepareValue ? $this->prepareQueryReference($value, $targetClass) : $value]];
1281+
}
1282+
1283+
return [[$fieldName, $prepareValue ? $this->convertToDatabaseValue($objectProperty, $value, $targetClass) : $value]];
1284+
}
1285+
12621286
/*
12631287
* 1 element: impossible (because of the dot)
12641288
* 2 elements: fieldName.objectProperty, fieldName.<index>, or fieldName.$. For EmbedMany and ReferenceMany, treat the second element as index if $inNewObj is true and convert the value. Otherwise, recurse.

0 commit comments

Comments
 (0)