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