-
Notifications
You must be signed in to change notification settings - Fork 1.5k
PHPORM-255 Enable disabling the id
to _id
field rename in embedded documents
#3332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3b4ad5a
f780906
48c14cc
68e57f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
use MongoDB\Builder\Type\SearchOperatorInterface; | ||
use MongoDB\Driver\Cursor; | ||
use MongoDB\Driver\ReadPreference; | ||
use MongoDB\Laravel\Connection; | ||
use Override; | ||
use RuntimeException; | ||
use stdClass; | ||
|
@@ -83,6 +84,7 @@ | |
use function trait_exists; | ||
use function var_export; | ||
|
||
/** @property Connection $connection */ | ||
class Builder extends BaseBuilder | ||
{ | ||
private const REGEX_DELIMITERS = ['/', '#', '~']; | ||
|
@@ -1764,9 +1766,9 @@ public function orWhereIntegerNotInRaw($column, $values, $boolean = 'and') | |
throw new BadMethodCallException('This method is not supported by MongoDB'); | ||
} | ||
|
||
private function aliasIdForQuery(array $values): array | ||
private function aliasIdForQuery(array $values, bool $root = true): array | ||
{ | ||
if (array_key_exists('id', $values)) { | ||
if (array_key_exists('id', $values) && ($root || $this->connection->getRenameEmbeddedIdField())) { | ||
if (array_key_exists('_id', $values) && $values['id'] !== $values['_id']) { | ||
throw new InvalidArgumentException('Cannot have both "id" and "_id" fields.'); | ||
} | ||
|
@@ -1793,7 +1795,7 @@ private function aliasIdForQuery(array $values): array | |
} | ||
|
||
// ".id" subfield are alias for "._id" | ||
if (str_ends_with($key, '.id')) { | ||
if (str_ends_with($key, '.id') && ($root || $this->connection->getRenameEmbeddedIdField())) { | ||
$newkey = substr($key, 0, -3) . '._id'; | ||
if (array_key_exists($newkey, $values) && $value !== $values[$newkey]) { | ||
throw new InvalidArgumentException(sprintf('Cannot have both "%s" and "%s" fields.', $key, $newkey)); | ||
|
@@ -1806,7 +1808,7 @@ private function aliasIdForQuery(array $values): array | |
|
||
foreach ($values as &$value) { | ||
if (is_array($value)) { | ||
$value = $this->aliasIdForQuery($value); | ||
$value = $this->aliasIdForQuery($value, false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that this appears to be the only recursive call to |
||
} elseif ($value instanceof DateTimeInterface) { | ||
$value = new UTCDateTime($value); | ||
} | ||
|
@@ -1824,10 +1826,13 @@ private function aliasIdForQuery(array $values): array | |
* | ||
* @template T of array|object | ||
*/ | ||
public function aliasIdForResult(array|object $values): array|object | ||
public function aliasIdForResult(array|object $values, bool $root = true): array|object | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There appear to be two calls to The context suggests that you're only processing a single document, but in one case you call Neither of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, that's fixed by using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to confirm, this fix was necessary to get the test case for multiple results passing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously, the function was called with |
||
{ | ||
if (is_array($values)) { | ||
if (array_key_exists('_id', $values) && ! array_key_exists('id', $values)) { | ||
if ( | ||
array_key_exists('_id', $values) && ! array_key_exists('id', $values) | ||
&& ($root || $this->connection->getRenameEmbeddedIdField()) | ||
) { | ||
$values['id'] = $values['_id']; | ||
unset($values['_id']); | ||
} | ||
|
@@ -1837,13 +1842,16 @@ public function aliasIdForResult(array|object $values): array|object | |
$values[$key] = Date::instance($value->toDateTime()) | ||
->setTimezone(new DateTimeZone(date_default_timezone_get())); | ||
} elseif (is_array($value) || is_object($value)) { | ||
$values[$key] = $this->aliasIdForResult($value); | ||
$values[$key] = $this->aliasIdForResult($value, false); | ||
} | ||
} | ||
} | ||
|
||
if ($values instanceof stdClass) { | ||
if (property_exists($values, '_id') && ! property_exists($values, 'id')) { | ||
if ( | ||
property_exists($values, '_id') && ! property_exists($values, 'id') | ||
&& ($root || $this->connection->getRenameEmbeddedIdField()) | ||
) { | ||
$values->id = $values->_id; | ||
unset($values->_id); | ||
} | ||
|
@@ -1853,7 +1861,7 @@ public function aliasIdForResult(array|object $values): array|object | |
$values->{$key} = Date::instance($value->toDateTime()) | ||
->setTimezone(new DateTimeZone(date_default_timezone_get())); | ||
} elseif (is_array($value) || is_object($value)) { | ||
$values->{$key} = $this->aliasIdForResult($value); | ||
$values->{$key} = $this->aliasIdForResult($value, false); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ | |
use Illuminate\Tests\Database\DatabaseQueryBuilderTest; | ||
use InvalidArgumentException; | ||
use LogicException; | ||
use Mockery as m; | ||
use MongoDB\BSON\Regex; | ||
use MongoDB\BSON\UTCDateTime; | ||
use MongoDB\Driver\ReadPreference; | ||
|
@@ -39,7 +38,7 @@ public function testMql(array $expected, Closure $build, ?string $requiredMethod | |
$this->markTestSkipped(sprintf('Method "%s::%s()" does not exist.', Builder::class, $requiredMethod)); | ||
} | ||
|
||
$builder = $build(self::getBuilder()); | ||
$builder = $build($this->getBuilder()); | ||
$this->assertInstanceOf(Builder::class, $builder); | ||
$mql = $builder->toMql(); | ||
|
||
|
@@ -1447,7 +1446,7 @@ function (Builder $elemMatchQuery): void { | |
#[DataProvider('provideExceptions')] | ||
public function testException($class, $message, Closure $build): void | ||
{ | ||
$builder = self::getBuilder(); | ||
$builder = $this->getBuilder(); | ||
|
||
$this->expectException($class); | ||
$this->expectExceptionMessage($message); | ||
|
@@ -1545,7 +1544,7 @@ public static function provideExceptions(): iterable | |
#[DataProvider('getEloquentMethodsNotSupported')] | ||
public function testEloquentMethodsNotSupported(Closure $callback) | ||
{ | ||
$builder = self::getBuilder(); | ||
$builder = $this->getBuilder(); | ||
|
||
$this->expectException(BadMethodCallException::class); | ||
$this->expectExceptionMessage('This method is not supported by MongoDB'); | ||
|
@@ -1600,12 +1599,38 @@ public static function getEloquentMethodsNotSupported() | |
yield 'orWhereIntegerNotInRaw' => [fn (Builder $builder) => $builder->orWhereIntegerNotInRaw('id', ['1a', 2])]; | ||
} | ||
|
||
private static function getBuilder(): Builder | ||
public function testRenameEmbeddedIdFieldCanBeDisabled() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this test exercise both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for this question, I added a full test to cover both. |
||
{ | ||
$connection = m::mock(Connection::class); | ||
$processor = m::mock(Processor::class); | ||
$connection->shouldReceive('getSession')->andReturn(null); | ||
$connection->shouldReceive('getQueryGrammar')->andReturn(new Grammar($connection)); | ||
$builder = $this->getBuilder(false); | ||
$this->assertFalse($builder->getConnection()->getRenameEmbeddedIdField()); | ||
|
||
$mql = $builder | ||
->where('id', '=', 10) | ||
->where('nested.id', '=', 20) | ||
->where('embed', '=', ['id' => 30]) | ||
->toMql(); | ||
|
||
$this->assertEquals([ | ||
'find' => [ | ||
[ | ||
'$and' => [ | ||
['_id' => 10], | ||
['nested.id' => 20], | ||
['embed' => ['id' => 30]], | ||
], | ||
], | ||
['typeMap' => ['root' => 'object', 'document' => 'array']], | ||
], | ||
], $mql); | ||
} | ||
|
||
private function getBuilder(bool $renameEmbeddedIdField = true): Builder | ||
{ | ||
$connection = $this->createStub(Connection::class); | ||
$connection->method('getRenameEmbeddedIdField')->willReturn($renameEmbeddedIdField); | ||
$processor = $this->createStub(Processor::class); | ||
$connection->method('getSession')->willReturn(null); | ||
$connection->method('getQueryGrammar')->willReturn(new Grammar($connection)); | ||
|
||
return new Builder($connection, null, $processor); | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.