29
29
use MongoDB \Builder \Type \SearchOperatorInterface ;
30
30
use MongoDB \Driver \Cursor ;
31
31
use MongoDB \Driver \ReadPreference ;
32
+ use MongoDB \Laravel \Connection ;
32
33
use Override ;
33
34
use RuntimeException ;
34
35
use stdClass ;
83
84
use function trait_exists ;
84
85
use function var_export ;
85
86
87
+ /** @method Connection getConnection() */
86
88
class Builder extends BaseBuilder
87
89
{
88
90
private const REGEX_DELIMITERS = ['/ ' , '# ' , '~ ' ];
@@ -124,6 +126,8 @@ class Builder extends BaseBuilder
124
126
*/
125
127
public $ options = [];
126
128
129
+ private ?bool $ renameEmbeddedIdField ;
130
+
127
131
/**
128
132
* All of the available clause operators.
129
133
*
@@ -1764,9 +1768,9 @@ public function orWhereIntegerNotInRaw($column, $values, $boolean = 'and')
1764
1768
throw new BadMethodCallException ('This method is not supported by MongoDB ' );
1765
1769
}
1766
1770
1767
- private function aliasIdForQuery (array $ values ): array
1771
+ private function aliasIdForQuery (array $ values, bool $ root = true ): array
1768
1772
{
1769
- if (array_key_exists ('id ' , $ values )) {
1773
+ if (array_key_exists ('id ' , $ values ) && ( $ root || $ this -> getConnection ()-> getRenameEmbeddedIdField ()) ) {
1770
1774
if (array_key_exists ('_id ' , $ values ) && $ values ['id ' ] !== $ values ['_id ' ]) {
1771
1775
throw new InvalidArgumentException ('Cannot have both "id" and "_id" fields. ' );
1772
1776
}
@@ -1793,7 +1797,7 @@ private function aliasIdForQuery(array $values): array
1793
1797
}
1794
1798
1795
1799
// ".id" subfield are alias for "._id"
1796
- if (str_ends_with ($ key , '.id ' )) {
1800
+ if (str_ends_with ($ key , '.id ' ) && ( $ root || $ this -> getConnection ()-> getRenameEmbeddedIdField ()) ) {
1797
1801
$ newkey = substr ($ key , 0 , -3 ) . '._id ' ;
1798
1802
if (array_key_exists ($ newkey , $ values ) && $ value !== $ values [$ newkey ]) {
1799
1803
throw new InvalidArgumentException (sprintf ('Cannot have both "%s" and "%s" fields. ' , $ key , $ newkey ));
@@ -1806,7 +1810,7 @@ private function aliasIdForQuery(array $values): array
1806
1810
1807
1811
foreach ($ values as &$ value ) {
1808
1812
if (is_array ($ value )) {
1809
- $ value = $ this ->aliasIdForQuery ($ value );
1813
+ $ value = $ this ->aliasIdForQuery ($ value, false );
1810
1814
} elseif ($ value instanceof DateTimeInterface) {
1811
1815
$ value = new UTCDateTime ($ value );
1812
1816
}
@@ -1824,10 +1828,13 @@ private function aliasIdForQuery(array $values): array
1824
1828
*
1825
1829
* @template T of array|object
1826
1830
*/
1827
- public function aliasIdForResult (array |object $ values ): array |object
1831
+ public function aliasIdForResult (array |object $ values, bool $ root = true ): array |object
1828
1832
{
1829
1833
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
+ ) {
1831
1838
$ values ['id ' ] = $ values ['_id ' ];
1832
1839
unset($ values ['_id ' ]);
1833
1840
}
@@ -1837,13 +1844,16 @@ public function aliasIdForResult(array|object $values): array|object
1837
1844
$ values [$ key ] = Date::instance ($ value ->toDateTime ())
1838
1845
->setTimezone (new DateTimeZone (date_default_timezone_get ()));
1839
1846
} elseif (is_array ($ value ) || is_object ($ value )) {
1840
- $ values [$ key ] = $ this ->aliasIdForResult ($ value );
1847
+ $ values [$ key ] = $ this ->aliasIdForResult ($ value, false );
1841
1848
}
1842
1849
}
1843
1850
}
1844
1851
1845
1852
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
+ ) {
1847
1857
$ values ->id = $ values ->_id ;
1848
1858
unset($ values ->_id );
1849
1859
}
@@ -1853,7 +1863,7 @@ public function aliasIdForResult(array|object $values): array|object
1853
1863
$ values ->{$ key } = Date::instance ($ value ->toDateTime ())
1854
1864
->setTimezone (new DateTimeZone (date_default_timezone_get ()));
1855
1865
} elseif (is_array ($ value ) || is_object ($ value )) {
1856
- $ values ->{$ key } = $ this ->aliasIdForResult ($ value );
1866
+ $ values ->{$ key } = $ this ->aliasIdForResult ($ value, false );
1857
1867
}
1858
1868
}
1859
1869
}
0 commit comments