32
32
use function array_map ;
33
33
use function array_merge ;
34
34
use function array_values ;
35
- use function array_walk_recursive ;
36
35
use function assert ;
37
36
use function blank ;
38
37
use function call_user_func ;
@@ -689,17 +688,7 @@ public function insert(array $values)
689
688
$ values = [$ values ];
690
689
}
691
690
692
- // Compatibility with Eloquent queries that uses "id" instead of MongoDB's _id
693
- foreach ($ values as &$ document ) {
694
- if (isset ($ document ['id ' ])) {
695
- if (isset ($ document ['_id ' ]) && $ document ['_id ' ] !== $ document ['id ' ]) {
696
- throw new InvalidArgumentException ('Cannot insert document with different "id" and "_id" values ' );
697
- }
698
-
699
- $ document ['_id ' ] = $ document ['id ' ];
700
- unset($ document ['id ' ]);
701
- }
702
- }
691
+ $ values = $ this ->aliasIdForQuery ($ values );
703
692
704
693
$ options = $ this ->inheritConnectionOptions ();
705
694
@@ -876,6 +865,7 @@ public function delete($id = null)
876
865
}
877
866
878
867
$ wheres = $ this ->compileWheres ();
868
+ $ wheres = $ this ->aliasIdForQuery ($ wheres );
879
869
$ options = $ this ->inheritConnectionOptions ();
880
870
881
871
if (is_int ($ this ->limit )) {
@@ -1070,16 +1060,12 @@ protected function performUpdate(array $update, array $options = [])
1070
1060
$ options ['multiple ' ] = true ;
1071
1061
}
1072
1062
1073
- // Since "id" is an alias for "_id", we prevent updating it
1074
- foreach ($ update as $ operator => $ fields ) {
1075
- if (array_key_exists ('id ' , $ fields )) {
1076
- throw new InvalidArgumentException ('Cannot update "id" field. ' );
1077
- }
1078
- }
1063
+ $ update = $ this ->aliasIdForQuery ($ update );
1079
1064
1080
1065
$ options = $ this ->inheritConnectionOptions ($ options );
1081
1066
1082
1067
$ wheres = $ this ->compileWheres ();
1068
+ $ wheres = $ this ->aliasIdForQuery ($ wheres );
1083
1069
$ result = $ this ->collection ->updateMany ($ wheres , $ update , $ options );
1084
1070
if ($ result ->isAcknowledged ()) {
1085
1071
return $ result ->getModifiedCount () ? $ result ->getModifiedCount () : $ result ->getUpsertedCount ();
@@ -1191,32 +1177,12 @@ protected function compileWheres(): array
1191
1177
}
1192
1178
}
1193
1179
1194
- // Convert DateTime values to UTCDateTime.
1195
- if (isset ($ where ['value ' ])) {
1196
- if (is_array ($ where ['value ' ])) {
1197
- array_walk_recursive ($ where ['value ' ], function (&$ item , $ key ) {
1198
- if ($ item instanceof DateTimeInterface) {
1199
- $ item = new UTCDateTime ($ item );
1200
- }
1201
- });
1202
- } else {
1203
- if ($ where ['value ' ] instanceof DateTimeInterface) {
1204
- $ where ['value ' ] = new UTCDateTime ($ where ['value ' ]);
1205
- }
1206
- }
1207
- } elseif (isset ($ where ['values ' ])) {
1208
- if (is_array ($ where ['values ' ])) {
1209
- array_walk_recursive ($ where ['values ' ], function (&$ item , $ key ) {
1210
- if ($ item instanceof DateTimeInterface) {
1211
- $ item = new UTCDateTime ($ item );
1212
- }
1213
- });
1214
- } elseif ($ where ['values ' ] instanceof CarbonPeriod) {
1215
- $ where ['values ' ] = [
1216
- new UTCDateTime ($ where ['values ' ]->getStartDate ()),
1217
- new UTCDateTime ($ where ['values ' ]->getEndDate ()),
1218
- ];
1219
- }
1180
+ // Convert CarbonPeriod to DateTime interval.
1181
+ if (isset ($ where ['values ' ]) && $ where ['values ' ] instanceof CarbonPeriod) {
1182
+ $ where ['values ' ] = [
1183
+ $ where ['values ' ]->getStartDate (),
1184
+ $ where ['values ' ]->getEndDate (),
1185
+ ];
1220
1186
}
1221
1187
1222
1188
// In a sequence of "where" clauses, the logical operator of the
@@ -1631,12 +1597,21 @@ public function orWhereIntegerNotInRaw($column, $values, $boolean = 'and')
1631
1597
private function aliasIdForQuery (array $ values ): array
1632
1598
{
1633
1599
if (array_key_exists ('id ' , $ values )) {
1600
+ if (array_key_exists ('_id ' , $ values )) {
1601
+ throw new InvalidArgumentException ('Cannot have both "id" and "_id" fields. ' );
1602
+ }
1603
+
1634
1604
$ values ['_id ' ] = $ values ['id ' ];
1635
1605
unset($ values ['id ' ]);
1636
1606
}
1637
1607
1638
1608
foreach ($ values as $ key => $ value ) {
1639
1609
if (is_string ($ key ) && str_ends_with ($ key , '.id ' )) {
1610
+ $ newkey = substr ($ key , 0 , -3 ) . '._id ' ;
1611
+ if (array_key_exists ($ newkey , $ values )) {
1612
+ throw new InvalidArgumentException (sprintf ('Cannot have both "%s" and "%s" fields. ' , $ key , $ newkey ));
1613
+ }
1614
+
1640
1615
$ values [substr ($ key , 0 , -3 ) . '._id ' ] = $ value ;
1641
1616
unset($ values [$ key ]);
1642
1617
}
@@ -1645,6 +1620,8 @@ private function aliasIdForQuery(array $values): array
1645
1620
foreach ($ values as &$ value ) {
1646
1621
if (is_array ($ value )) {
1647
1622
$ value = $ this ->aliasIdForQuery ($ value );
1623
+ } elseif ($ value instanceof DateTimeInterface) {
1624
+ $ value = new UTCDateTime ($ value );
1648
1625
}
1649
1626
}
1650
1627
0 commit comments