39
39
use MongoDB \GridFS \Bucket ;
40
40
use stdClass ;
41
41
42
+ use function array_column ;
42
43
use function array_combine ;
43
44
use function array_fill ;
44
45
use function array_intersect_key ;
@@ -1067,10 +1068,10 @@ public function prepareQueryOrNewObj(array $query, bool $isNewObj = false): arra
1067
1068
}
1068
1069
1069
1070
$ preparedQueryElements = $ this ->prepareQueryElement ($ key , $ value , null , true , $ isNewObj );
1070
- foreach ( $ preparedQueryElements as [ $ preparedKey , $ preparedValue ]) {
1071
- $ preparedValue = $ this -> convertToDatabaseValue ( $ key , $ preparedValue );
1072
- $ preparedQuery [ $ preparedKey ] = $ preparedValue ;
1073
- }
1071
+ $ preparedQuery = array_combine (
1072
+ array_column ( $ preparedQueryElements , 0 ),
1073
+ array_column ( $ preparedQueryElements , 1 ),
1074
+ );
1074
1075
}
1075
1076
1076
1077
return $ preparedQuery ;
@@ -1083,8 +1084,10 @@ public function prepareQueryOrNewObj(array $query, bool $isNewObj = false): arra
1083
1084
*
1084
1085
* @return mixed
1085
1086
*/
1086
- private function convertToDatabaseValue (string $ fieldName , $ value )
1087
+ private function convertToDatabaseValue (string $ fieldName , $ value, ? ClassMetadata $ class = null )
1087
1088
{
1089
+ $ class ??= $ this ->class ;
1090
+
1088
1091
if (is_array ($ value )) {
1089
1092
foreach ($ value as $ k => $ v ) {
1090
1093
if ($ k === '$exists ' || $ k === '$type ' || $ k === '$currentDate ' ) {
@@ -1097,15 +1100,15 @@ private function convertToDatabaseValue(string $fieldName, $value)
1097
1100
return $ value ;
1098
1101
}
1099
1102
1100
- if (! $ this -> class ->hasField ($ fieldName )) {
1103
+ if (! $ class ->hasField ($ fieldName )) {
1101
1104
if ($ value instanceof BackedEnum) {
1102
1105
$ value = $ value ->value ;
1103
1106
}
1104
1107
1105
1108
return Type::convertPHPToDatabaseValue ($ value );
1106
1109
}
1107
1110
1108
- $ mapping = $ this -> class ->fieldMappings [$ fieldName ];
1111
+ $ mapping = $ class ->fieldMappings [$ fieldName ];
1109
1112
$ typeName = $ mapping ['type ' ];
1110
1113
1111
1114
if (! empty ($ mapping ['reference ' ]) || ! empty ($ mapping ['embedded ' ])) {
@@ -1143,15 +1146,15 @@ private function convertToDatabaseValue(string $fieldName, $value)
1143
1146
*
1144
1147
* @return array<array{string, mixed}>
1145
1148
*/
1146
- private function prepareQueryElement (string $ fieldName , $ value = null , ?ClassMetadata $ class = null , bool $ prepareValue = true , bool $ inNewObj = false ): array
1149
+ private function prepareQueryElement (string $ originalFieldName , $ value = null , ?ClassMetadata $ class = null , bool $ prepareValue = true , bool $ inNewObj = false ): array
1147
1150
{
1148
1151
$ class ??= $ this ->class ;
1149
1152
1150
1153
// @todo Consider inlining calls to ClassMetadata methods
1151
1154
1152
1155
// Process all non-identifier fields by translating field names
1153
- if ($ class ->hasField ($ fieldName ) && ! $ class ->isIdentifier ($ fieldName )) {
1154
- $ mapping = $ class ->fieldMappings [$ fieldName ];
1156
+ if ($ class ->hasField ($ originalFieldName ) && ! $ class ->isIdentifier ($ originalFieldName )) {
1157
+ $ mapping = $ class ->fieldMappings [$ originalFieldName ];
1155
1158
$ fieldName = $ mapping ['name ' ];
1156
1159
1157
1160
if (! $ prepareValue ) {
@@ -1176,7 +1179,7 @@ private function prepareQueryElement(string $fieldName, $value = null, ?ClassMet
1176
1179
1177
1180
// No further preparation unless we're dealing with a simple reference
1178
1181
if (empty ($ mapping ['reference ' ]) || $ mapping ['storeAs ' ] !== ClassMetadata::REFERENCE_STORE_AS_ID || empty ((array ) $ value )) {
1179
- return [[$ fieldName , $ value ]];
1182
+ return [[$ fieldName , $ this -> convertToDatabaseValue ( $ originalFieldName , $ value, $ class ) ]];
1180
1183
}
1181
1184
1182
1185
// Additional preparation for one or more simple reference values
@@ -1195,7 +1198,7 @@ private function prepareQueryElement(string $fieldName, $value = null, ?ClassMet
1195
1198
}
1196
1199
1197
1200
// Process identifier fields
1198
- if (($ class ->hasField ($ fieldName ) && $ class ->isIdentifier ($ fieldName )) || $ fieldName === '_id ' ) {
1201
+ if (($ class ->hasField ($ originalFieldName ) && $ class ->isIdentifier ($ originalFieldName )) || $ originalFieldName === '_id ' ) {
1199
1202
$ fieldName = '_id ' ;
1200
1203
1201
1204
if (! $ prepareValue ) {
@@ -1215,8 +1218,8 @@ private function prepareQueryElement(string $fieldName, $value = null, ?ClassMet
1215
1218
}
1216
1219
1217
1220
// No processing for unmapped, non-identifier, non-dotted field names
1218
- if (strpos ($ fieldName , '. ' ) === false ) {
1219
- return [[$ fieldName , $ value ]];
1221
+ if (strpos ($ originalFieldName , '. ' ) === false ) {
1222
+ return [[$ originalFieldName , $ value ]];
1220
1223
}
1221
1224
1222
1225
/* Process "fieldName.objectProperty" queries (on arrays or objects).
@@ -1225,11 +1228,11 @@ private function prepareQueryElement(string $fieldName, $value = null, ?ClassMet
1225
1228
* significant: "fieldName.objectProperty" with an optional index or key
1226
1229
* for collections stored as either BSON arrays or objects.
1227
1230
*/
1228
- $ e = explode ('. ' , $ fieldName , 4 );
1231
+ $ e = explode ('. ' , $ originalFieldName , 4 );
1229
1232
1230
1233
// No further processing for unmapped fields
1231
1234
if (! isset ($ class ->fieldMappings [$ e [0 ]])) {
1232
- return [[$ fieldName , $ value ]];
1235
+ return [[$ originalFieldName , $ value ]];
1233
1236
}
1234
1237
1235
1238
$ mapping = $ class ->fieldMappings [$ e [0 ]];
@@ -1246,6 +1249,7 @@ private function prepareQueryElement(string $fieldName, $value = null, ?ClassMet
1246
1249
$ mapping ['type ' ] === ClassMetadata::MANY && CollectionHelper::isHash ($ mapping ['strategy ' ])
1247
1250
&& isset ($ e [2 ])
1248
1251
) {
1252
+ $ fieldName = $ originalFieldName ;
1249
1253
$ objectProperty = $ e [2 ];
1250
1254
$ objectPropertyPrefix = $ e [1 ] . '. ' ;
1251
1255
$ nextObjectProperty = implode ('. ' , array_slice ($ e , 3 ));
@@ -1339,7 +1343,7 @@ private function prepareQueryElement(string $fieldName, $value = null, ?ClassMet
1339
1343
}, $ fieldNames );
1340
1344
}
1341
1345
1342
- return [[$ fieldName , $ value ]];
1346
+ return [[$ fieldName , $ this -> convertToDatabaseValue ( $ objectProperty , $ value, $ targetClass ) ]];
1343
1347
}
1344
1348
1345
1349
/**
0 commit comments