2929use MongoDB \Builder \Type \SearchOperatorInterface ;
3030use MongoDB \Driver \Cursor ;
3131use MongoDB \Driver \ReadPreference ;
32+ use MongoDB \Laravel \Connection ;
3233use Override ;
3334use RuntimeException ;
3435use stdClass ;
8283use function trait_exists ;
8384use function var_export ;
8485
86+ /** @method Connection getConnection() */
8587class Builder extends BaseBuilder
8688{
8789 private const REGEX_DELIMITERS = ['/ ' , '# ' , '~ ' ];
@@ -123,6 +125,8 @@ class Builder extends BaseBuilder
123125 */
124126 public $ options = [];
125127
128+ private ?bool $ renameEmbeddedIdField ;
129+
126130 /**
127131 * All of the available clause operators.
128132 *
@@ -1763,9 +1767,9 @@ public function orWhereIntegerNotInRaw($column, $values, $boolean = 'and')
17631767 throw new BadMethodCallException ('This method is not supported by MongoDB ' );
17641768 }
17651769
1766- private function aliasIdForQuery (array $ values ): array
1770+ private function aliasIdForQuery (array $ values, bool $ root = true ): array
17671771 {
1768- if (array_key_exists ('id ' , $ values )) {
1772+ if (array_key_exists ('id ' , $ values ) && ( $ root || $ this -> getConnection ()-> getRenameEmbeddedIdField ()) ) {
17691773 if (array_key_exists ('_id ' , $ values ) && $ values ['id ' ] !== $ values ['_id ' ]) {
17701774 throw new InvalidArgumentException ('Cannot have both "id" and "_id" fields. ' );
17711775 }
@@ -1792,7 +1796,7 @@ private function aliasIdForQuery(array $values): array
17921796 }
17931797
17941798 // ".id" subfield are alias for "._id"
1795- if (str_ends_with ($ key , '.id ' )) {
1799+ if (str_ends_with ($ key , '.id ' ) && ( $ root || $ this -> getConnection ()-> getRenameEmbeddedIdField ()) ) {
17961800 $ newkey = substr ($ key , 0 , -3 ) . '._id ' ;
17971801 if (array_key_exists ($ newkey , $ values ) && $ value !== $ values [$ newkey ]) {
17981802 throw new InvalidArgumentException (sprintf ('Cannot have both "%s" and "%s" fields. ' , $ key , $ newkey ));
@@ -1805,7 +1809,7 @@ private function aliasIdForQuery(array $values): array
18051809
18061810 foreach ($ values as &$ value ) {
18071811 if (is_array ($ value )) {
1808- $ value = $ this ->aliasIdForQuery ($ value );
1812+ $ value = $ this ->aliasIdForQuery ($ value, false );
18091813 } elseif ($ value instanceof DateTimeInterface) {
18101814 $ value = new UTCDateTime ($ value );
18111815 }
@@ -1823,10 +1827,13 @@ private function aliasIdForQuery(array $values): array
18231827 *
18241828 * @template T of array|object
18251829 */
1826- public function aliasIdForResult (array |object $ values ): array |object
1830+ public function aliasIdForResult (array |object $ values, bool $ root = true ): array |object
18271831 {
18281832 if (is_array ($ values )) {
1829- if (array_key_exists ('_id ' , $ values ) && ! array_key_exists ('id ' , $ values )) {
1833+ if (
1834+ array_key_exists ('_id ' , $ values ) && ! array_key_exists ('id ' , $ values )
1835+ && ($ root || $ this ->getConnection ()->getRenameEmbeddedIdField ())
1836+ ) {
18301837 $ values ['id ' ] = $ values ['_id ' ];
18311838 unset($ values ['_id ' ]);
18321839 }
@@ -1836,13 +1843,16 @@ public function aliasIdForResult(array|object $values): array|object
18361843 $ values [$ key ] = Date::instance ($ value ->toDateTime ())
18371844 ->setTimezone (new DateTimeZone (date_default_timezone_get ()));
18381845 } elseif (is_array ($ value ) || is_object ($ value )) {
1839- $ values [$ key ] = $ this ->aliasIdForResult ($ value );
1846+ $ values [$ key ] = $ this ->aliasIdForResult ($ value, false );
18401847 }
18411848 }
18421849 }
18431850
18441851 if ($ values instanceof stdClass) {
1845- if (property_exists ($ values , '_id ' ) && ! property_exists ($ values , 'id ' )) {
1852+ if (
1853+ property_exists ($ values , '_id ' ) && ! property_exists ($ values , 'id ' )
1854+ && ($ root || $ this ->getConnection ()->getRenameEmbeddedIdField ())
1855+ ) {
18461856 $ values ->id = $ values ->_id ;
18471857 unset($ values ->_id );
18481858 }
@@ -1852,7 +1862,7 @@ public function aliasIdForResult(array|object $values): array|object
18521862 $ values ->{$ key } = Date::instance ($ value ->toDateTime ())
18531863 ->setTimezone (new DateTimeZone (date_default_timezone_get ()));
18541864 } elseif (is_array ($ value ) || is_object ($ value )) {
1855- $ values ->{$ key } = $ this ->aliasIdForResult ($ value );
1865+ $ values ->{$ key } = $ this ->aliasIdForResult ($ value, false );
18561866 }
18571867 }
18581868 }
0 commit comments