@@ -1068,7 +1068,6 @@ public function prepareQueryOrNewObj(array $query, bool $isNewObj = false): arra
1068
1068
1069
1069
$ preparedQueryElements = $ this ->prepareQueryElement ($ key , $ value , null , true , $ isNewObj );
1070
1070
foreach ($ preparedQueryElements as [$ preparedKey , $ preparedValue ]) {
1071
- $ preparedValue = $ this ->convertToDatabaseValue ($ key , $ preparedValue );
1072
1071
$ preparedQuery [$ preparedKey ] = $ preparedValue ;
1073
1072
}
1074
1073
}
@@ -1083,29 +1082,31 @@ public function prepareQueryOrNewObj(array $query, bool $isNewObj = false): arra
1083
1082
*
1084
1083
* @return mixed
1085
1084
*/
1086
- private function convertToDatabaseValue (string $ fieldName , $ value )
1085
+ private function convertToDatabaseValue (string $ fieldName , $ value, ? ClassMetadata $ class = null )
1087
1086
{
1087
+ $ class ??= $ this ->class ;
1088
+
1088
1089
if (is_array ($ value )) {
1089
1090
foreach ($ value as $ k => $ v ) {
1090
1091
if ($ k === '$exists ' || $ k === '$type ' || $ k === '$currentDate ' ) {
1091
1092
continue ;
1092
1093
}
1093
1094
1094
- $ value [$ k ] = $ this ->convertToDatabaseValue ($ fieldName , $ v );
1095
+ $ value [$ k ] = $ this ->convertToDatabaseValue ($ fieldName , $ v, $ class );
1095
1096
}
1096
1097
1097
1098
return $ value ;
1098
1099
}
1099
1100
1100
- if (! $ this -> class ->hasField ($ fieldName )) {
1101
+ if (! $ class ->hasField ($ fieldName )) {
1101
1102
if ($ value instanceof BackedEnum) {
1102
1103
$ value = $ value ->value ;
1103
1104
}
1104
1105
1105
1106
return Type::convertPHPToDatabaseValue ($ value );
1106
1107
}
1107
1108
1108
- $ mapping = $ this -> class ->fieldMappings [$ fieldName ];
1109
+ $ mapping = $ class ->fieldMappings [$ fieldName ];
1109
1110
$ typeName = $ mapping ['type ' ];
1110
1111
1111
1112
if (! empty ($ mapping ['reference ' ]) || ! empty ($ mapping ['embedded ' ])) {
@@ -1143,15 +1144,15 @@ private function convertToDatabaseValue(string $fieldName, $value)
1143
1144
*
1144
1145
* @return array<array{string, mixed}>
1145
1146
*/
1146
- private function prepareQueryElement (string $ fieldName , $ value = null , ?ClassMetadata $ class = null , bool $ prepareValue = true , bool $ inNewObj = false ): array
1147
+ private function prepareQueryElement (string $ originalFieldName , $ value = null , ?ClassMetadata $ class = null , bool $ prepareValue = true , bool $ inNewObj = false ): array
1147
1148
{
1148
1149
$ class ??= $ this ->class ;
1149
1150
1150
1151
// @todo Consider inlining calls to ClassMetadata methods
1151
1152
1152
1153
// Process all non-identifier fields by translating field names
1153
- if ($ class ->hasField ($ fieldName ) && ! $ class ->isIdentifier ($ fieldName )) {
1154
- $ mapping = $ class ->fieldMappings [$ fieldName ];
1154
+ if ($ class ->hasField ($ originalFieldName ) && ! $ class ->isIdentifier ($ originalFieldName )) {
1155
+ $ mapping = $ class ->fieldMappings [$ originalFieldName ];
1155
1156
$ fieldName = $ mapping ['name ' ];
1156
1157
1157
1158
if (! $ prepareValue ) {
@@ -1176,7 +1177,7 @@ private function prepareQueryElement(string $fieldName, $value = null, ?ClassMet
1176
1177
1177
1178
// No further preparation unless we're dealing with a simple reference
1178
1179
if (empty ($ mapping ['reference ' ]) || $ mapping ['storeAs ' ] !== ClassMetadata::REFERENCE_STORE_AS_ID || empty ((array ) $ value )) {
1179
- return [[$ fieldName , $ value ]];
1180
+ return [[$ fieldName , $ this -> convertToDatabaseValue ( $ originalFieldName , $ value, $ class ) ]];
1180
1181
}
1181
1182
1182
1183
// Additional preparation for one or more simple reference values
@@ -1195,7 +1196,7 @@ private function prepareQueryElement(string $fieldName, $value = null, ?ClassMet
1195
1196
}
1196
1197
1197
1198
// Process identifier fields
1198
- if (($ class ->hasField ($ fieldName ) && $ class ->isIdentifier ($ fieldName )) || $ fieldName === '_id ' ) {
1199
+ if (($ class ->hasField ($ originalFieldName ) && $ class ->isIdentifier ($ originalFieldName )) || $ originalFieldName === '_id ' ) {
1199
1200
$ fieldName = '_id ' ;
1200
1201
1201
1202
if (! $ prepareValue ) {
@@ -1215,8 +1216,8 @@ private function prepareQueryElement(string $fieldName, $value = null, ?ClassMet
1215
1216
}
1216
1217
1217
1218
// No processing for unmapped, non-identifier, non-dotted field names
1218
- if (strpos ($ fieldName , '. ' ) === false ) {
1219
- return [[$ fieldName , $ value ]];
1219
+ if (strpos ($ originalFieldName , '. ' ) === false ) {
1220
+ return [[$ originalFieldName , $ prepareValue ? $ this -> convertToDatabaseValue ( $ originalFieldName , $ value , $ class ) : $ value ]];
1220
1221
}
1221
1222
1222
1223
/* Process "fieldName.objectProperty" queries (on arrays or objects).
@@ -1225,11 +1226,11 @@ private function prepareQueryElement(string $fieldName, $value = null, ?ClassMet
1225
1226
* significant: "fieldName.objectProperty" with an optional index or key
1226
1227
* for collections stored as either BSON arrays or objects.
1227
1228
*/
1228
- $ e = explode ('. ' , $ fieldName , 4 );
1229
+ $ e = explode ('. ' , $ originalFieldName , 4 );
1229
1230
1230
1231
// No further processing for unmapped fields
1231
1232
if (! isset ($ class ->fieldMappings [$ e [0 ]])) {
1232
- return [[$ fieldName , $ value ]];
1233
+ return [[$ originalFieldName , $ prepareValue ? $ this -> convertToDatabaseValue ( $ e [ 0 ], $ value , $ class ) : $ value ]];
1233
1234
}
1234
1235
1235
1236
$ mapping = $ class ->fieldMappings [$ e [0 ]];
@@ -1246,6 +1247,7 @@ private function prepareQueryElement(string $fieldName, $value = null, ?ClassMet
1246
1247
$ mapping ['type ' ] === ClassMetadata::MANY && CollectionHelper::isHash ($ mapping ['strategy ' ])
1247
1248
&& isset ($ e [2 ])
1248
1249
) {
1250
+ $ fieldName = $ originalFieldName ;
1249
1251
$ objectProperty = $ e [2 ];
1250
1252
$ objectPropertyPrefix = $ e [1 ] . '. ' ;
1251
1253
$ nextObjectProperty = implode ('. ' , array_slice ($ e , 3 ));
@@ -1262,7 +1264,7 @@ private function prepareQueryElement(string $fieldName, $value = null, ?ClassMet
1262
1264
} else {
1263
1265
$ fieldName = $ e [0 ] . '. ' . $ e [1 ];
1264
1266
1265
- return [[$ fieldName , $ value ]];
1267
+ return [[$ fieldName , $ prepareValue ? $ this -> convertToDatabaseValue ( $ e [ 0 ], $ value , $ class ) : $ value ]];
1266
1268
}
1267
1269
1268
1270
// No further processing for fields without a targetDocument mapping
@@ -1271,7 +1273,7 @@ private function prepareQueryElement(string $fieldName, $value = null, ?ClassMet
1271
1273
$ fieldName .= '. ' . $ nextObjectProperty ;
1272
1274
}
1273
1275
1274
- return [[$ fieldName , $ value ]];
1276
+ return [[$ fieldName , $ prepareValue ? $ this -> convertToDatabaseValue ( $ e [ 0 ], $ value , $ class ) : $ value ]];
1275
1277
}
1276
1278
1277
1279
$ targetClass = $ this ->dm ->getClassMetadata ($ mapping ['targetDocument ' ]);
@@ -1282,7 +1284,7 @@ private function prepareQueryElement(string $fieldName, $value = null, ?ClassMet
1282
1284
$ fieldName .= '. ' . $ nextObjectProperty ;
1283
1285
}
1284
1286
1285
- return [[$ fieldName , $ value ]];
1287
+ return [[$ fieldName , $ prepareValue ? $ this -> convertToDatabaseValue ( $ objectProperty , $ value , $ targetClass ) : $ value ]];
1286
1288
}
1287
1289
1288
1290
$ targetMapping = $ targetClass ->getFieldMapping ($ objectProperty );
@@ -1329,7 +1331,7 @@ private function prepareQueryElement(string $fieldName, $value = null, ?ClassMet
1329
1331
$ nextObjectProperty = '$ ' . $ nextObjectProperty ;
1330
1332
}
1331
1333
1332
- $ fieldNames = [[$ nextObjectProperty , $ value ]];
1334
+ $ fieldNames = [[$ nextObjectProperty , $ prepareValue ? $ this -> convertToDatabaseValue ( $ nextObjectProperty , $ value , $ nextTargetClass ) : $ value ]];
1333
1335
}
1334
1336
1335
1337
return array_map (static function ($ preparedTuple ) use ($ fieldName ) {
@@ -1339,7 +1341,7 @@ private function prepareQueryElement(string $fieldName, $value = null, ?ClassMet
1339
1341
}, $ fieldNames );
1340
1342
}
1341
1343
1342
- return [[$ fieldName , $ value ]];
1344
+ return [[$ fieldName , $ this -> convertToDatabaseValue ( $ objectProperty , $ value, $ targetClass ) ]];
1343
1345
}
1344
1346
1345
1347
/**
0 commit comments