-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
fix attributeToArray method #3024
Conversation
Thank you @florianJacques for contributing. To review your PR correctly, I need that you describe the issue, and if possible add tests validating the fix. |
Test ok |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new test does not cover the issue, it works without your fix.
In order to convert date objects nested into embedded object (not embedded model), you can define a regular cast using a dot for the field path.
Example:
laravel-mongodb/tests/Models/User.php
Lines 42 to 46 in ffacc6b
protected $casts = [ | |
'birthday' => 'datetime', | |
'entry.date' => 'datetime', | |
'member_status' => MemberStatus::class, | |
]; |
Validated by this test:
laravel-mongodb/tests/ModelTest.php
Lines 774 to 776 in ffacc6b
// Test with create and array property | |
$user = User::create(['name' => 'Jane Doe', 'entry' => ['date' => $date]]); | |
$this->assertInstanceOf(Carbon::class, $user->getAttribute('entry.date')); |
src/Eloquent/Model.php
Outdated
@@ -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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok now is split
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Co-authored-by: Jérôme Tamarelle <jerome@tamarelle.net>
Co-authored-by: Jérôme Tamarelle <jerome@tamarelle.net>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking of rejecting your proposal.
Embeds should not be used in this way. Instead of calling $results['addresses']
, you need to call $addresses->addresses()->get()
which returns a collection of Address
objects by applying the castings defined in the Model.
You can also work with objects directly, in which case they are not embedded and don't automatically have an id, but you can add one.
$value = (string) $value; | ||
} elseif ($value instanceof Binary) { | ||
$value = (string) $value->getData(); | ||
} elseif ($value instanceof UTCDateTime) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
When dealing with embedded models with relationships (EmbedOne and/or EmbedMany), the serialization methods do not serialize Mongo BSON types in nested fields, as the function only converts top-level attributes.
In the exemple, prices is embedMany Relationship
Before fix =>
After fix =>