Skip to content

fix attributeToArray method #3024

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

Closed
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,22 @@ public function attributesToArray()
// MongoDB related objects to a string representation. This kind
// of mimics the SQL behaviour so that dates are formatted
// nicely when your models are converted to JSON.
foreach ($attributes as $key => &$value) {
$convertMongoObjects = function (&$value) use (&$convertMongoObjects) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a private method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok now is split

Copy link
Contributor Author

@florianJacques florianJacques Jul 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the test, I simply reused existing model. Would you like me to create a new one?
Yes for nested fields (with no embed relationship), but will it work without my patch for more deep level fields?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you can create a new model class for the test.

if ($value instanceof ObjectID) {
$value = (string) $value;
} elseif ($value instanceof Binary) {
$value = (string) $value->getData();
} elseif ($value instanceof UTCDateTime) {
$value = $this->serializeDate($value->toDateTime());
} elseif (is_array($value)) {
foreach ($value as &$embedValue) {
$convertMongoObjects($embedValue);
}
}
};

foreach ($attributes as $key => &$value) {
$convertMongoObjects($value);
}

return $attributes;
Expand Down
10 changes: 10 additions & 0 deletions tests/EmbeddedRelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use MongoDB\Laravel\Tests\Models\User;

use function array_merge;
use function PHPUnit\Framework\assertIsString;

class EmbeddedRelationsTest extends TestCase
{
Expand Down Expand Up @@ -945,4 +946,13 @@ public function testGetQueueableRelationsEmbedsOne()
$this->assertEquals(['father'], $user->getQueueableRelations());
$this->assertEquals([], $user->father->getQueueableRelations());
}

public function testEmbedManySerialization()
{
$user = User::create(['name' => 'John Doe']);
$user->addresses()->save(new Address(['city' => 'New York']));

$results = $user->toArray();
assertIsString($results['addresses'][0]['_id']);
}
}
Loading