2929use MongoDB \Builder \Type \SearchOperatorInterface ;
3030use MongoDB \Driver \Cursor ;
3131use MongoDB \Driver \ReadPreference ;
32+ use MongoDB \Laravel \Connection ;
3233use Override ;
3334use RuntimeException ;
3435use stdClass ;
8384use function trait_exists ;
8485use function var_export ;
8586
87+ /** @method Connection getConnection() */
8688class Builder extends BaseBuilder
8789{
8890 private const REGEX_DELIMITERS = ['/ ' , '# ' , '~ ' ];
@@ -124,6 +126,8 @@ class Builder extends BaseBuilder
124126 */
125127 public $ options = [];
126128
129+ private ?bool $ renameEmbeddedIdField ;
130+
127131 /**
128132 * All of the available clause operators.
129133 *
@@ -1764,9 +1768,9 @@ public function orWhereIntegerNotInRaw($column, $values, $boolean = 'and')
17641768 throw new BadMethodCallException ('This method is not supported by MongoDB ' );
17651769 }
17661770
1767- private function aliasIdForQuery (array $ values ): array
1771+ private function aliasIdForQuery (array $ values, bool $ root = true ): array
17681772 {
1769- if (array_key_exists ('id ' , $ values )) {
1773+ if (array_key_exists ('id ' , $ values ) && ( $ root || $ this -> getConnection ()-> getRenameEmbeddedIdField ()) ) {
17701774 if (array_key_exists ('_id ' , $ values ) && $ values ['id ' ] !== $ values ['_id ' ]) {
17711775 throw new InvalidArgumentException ('Cannot have both "id" and "_id" fields. ' );
17721776 }
@@ -1793,7 +1797,7 @@ private function aliasIdForQuery(array $values): array
17931797 }
17941798
17951799 // ".id" subfield are alias for "._id"
1796- if (str_ends_with ($ key , '.id ' )) {
1800+ if (str_ends_with ($ key , '.id ' ) && ( $ root || $ this -> getConnection ()-> getRenameEmbeddedIdField ()) ) {
17971801 $ newkey = substr ($ key , 0 , -3 ) . '._id ' ;
17981802 if (array_key_exists ($ newkey , $ values ) && $ value !== $ values [$ newkey ]) {
17991803 throw new InvalidArgumentException (sprintf ('Cannot have both "%s" and "%s" fields. ' , $ key , $ newkey ));
@@ -1806,7 +1810,7 @@ private function aliasIdForQuery(array $values): array
18061810
18071811 foreach ($ values as &$ value ) {
18081812 if (is_array ($ value )) {
1809- $ value = $ this ->aliasIdForQuery ($ value );
1813+ $ value = $ this ->aliasIdForQuery ($ value, false );
18101814 } elseif ($ value instanceof DateTimeInterface) {
18111815 $ value = new UTCDateTime ($ value );
18121816 }
@@ -1824,10 +1828,13 @@ private function aliasIdForQuery(array $values): array
18241828 *
18251829 * @template T of array|object
18261830 */
1827- public function aliasIdForResult (array |object $ values ): array |object
1831+ public function aliasIdForResult (array |object $ values, bool $ root = true ): array |object
18281832 {
18291833 if (is_array ($ values )) {
1830- if (array_key_exists ('_id ' , $ values ) && ! array_key_exists ('id ' , $ values )) {
1834+ if (
1835+ array_key_exists ('_id ' , $ values ) && ! array_key_exists ('id ' , $ values )
1836+ && ($ root || $ this ->getConnection ()->getRenameEmbeddedIdField ())
1837+ ) {
18311838 $ values ['id ' ] = $ values ['_id ' ];
18321839 unset($ values ['_id ' ]);
18331840 }
@@ -1837,13 +1844,16 @@ public function aliasIdForResult(array|object $values): array|object
18371844 $ values [$ key ] = Date::instance ($ value ->toDateTime ())
18381845 ->setTimezone (new DateTimeZone (date_default_timezone_get ()));
18391846 } elseif (is_array ($ value ) || is_object ($ value )) {
1840- $ values [$ key ] = $ this ->aliasIdForResult ($ value );
1847+ $ values [$ key ] = $ this ->aliasIdForResult ($ value, false );
18411848 }
18421849 }
18431850 }
18441851
18451852 if ($ values instanceof stdClass) {
1846- if (property_exists ($ values , '_id ' ) && ! property_exists ($ values , 'id ' )) {
1853+ if (
1854+ property_exists ($ values , '_id ' ) && ! property_exists ($ values , 'id ' )
1855+ && ($ root || $ this ->getConnection ()->getRenameEmbeddedIdField ())
1856+ ) {
18471857 $ values ->id = $ values ->_id ;
18481858 unset($ values ->_id );
18491859 }
@@ -1853,7 +1863,7 @@ public function aliasIdForResult(array|object $values): array|object
18531863 $ values ->{$ key } = Date::instance ($ value ->toDateTime ())
18541864 ->setTimezone (new DateTimeZone (date_default_timezone_get ()));
18551865 } elseif (is_array ($ value ) || is_object ($ value )) {
1856- $ values ->{$ key } = $ this ->aliasIdForResult ($ value );
1866+ $ values ->{$ key } = $ this ->aliasIdForResult ($ value, false );
18571867 }
18581868 }
18591869 }
0 commit comments