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 all 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
34 changes: 29 additions & 5 deletions src/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,34 @@ protected function fromDecimal($value, $decimals)
return new Decimal128($this->asDecimal($value, $decimals));
}

/**
* Convert MongoDB objects to their string representations.
*
* This method converts MongoDB related objects (ObjectID, Binary, UTCDateTime)
* to their serialized representations, ensuring the values can be correctly
* serialized when the model is converted to JSON.
*
* @param mixed $value The value to convert
*
* @return mixed
*/
protected function convertMongoObjects(mixed $value): mixed
{
if ($value instanceof ObjectID) {
$value = (string) $value;
} elseif ($value instanceof Binary) {
$value = (string) $value->getData();
} elseif ($value instanceof UTCDateTime) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think dates should be handled here. They were not in the current code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I understand, indeed if relationships are called, casts are automatic. Sorry for the misunderstanding.
You can reject the fix proposal. Looking forward to contributing on something else.

$value = $this->serializeDate($value->toDateTime());
} elseif (is_array($value)) {
foreach ($value as &$nestedValue) {
$nestedValue = $this->convertMongoObjects($nestedValue);
}
}

return $value;
}

/** @inheritdoc */
public function attributesToArray()
{
Expand All @@ -329,11 +357,7 @@ public function attributesToArray()
// of mimics the SQL behaviour so that dates are formatted
// nicely when your models are converted to JSON.
foreach ($attributes as $key => &$value) {
if ($value instanceof ObjectID) {
$value = (string) $value;
} elseif ($value instanceof Binary) {
$value = (string) $value->getData();
}
$value = $this->convertMongoObjects($value);
}

return $attributes;
Expand Down
13 changes: 13 additions & 0 deletions tests/EmbeddedRelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function tearDown(): void
Client::truncate();
Group::truncate();
Photo::truncate();
Address::truncate();
}

public function testEmbedsManySave()
Expand Down Expand Up @@ -945,4 +946,16 @@ public function testGetQueueableRelationsEmbedsOne()
$this->assertEquals(['father'], $user->getQueueableRelations());
$this->assertEquals([], $user->father->getQueueableRelations());
}

public function testEmbedManySerialization()
{
$address = new Address(['country' => 'France']);
$address->save();

$address->addresses()->create(['city' => 'Paris']);
$address->addresses()->create(['city' => 'Nice']);

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