-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Fix overwriting updated_at
with $set
#3433
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
Conversation
UPD: 2nd commit is not solution. My fault. |
how about just create a new field if you have custom updated timestamp like modified_date while leaving default behavior of timestamps? or modify the constant of UPDATED_AT or if its on specific Model just use withoutTimestamps |
Hello @masterbater, Turning off timestamp - it's not solution for first issue. I still need
If it's related to 2nd issue, not sure that it's good to ignore property from parent class (trait). /**
* The storage format of the model's date columns.
*
* @var string
*/
protected $dateFormat; I guessed better to not ignore this property, I faced unexpected behaviour, and it's true, doesn't it? |
Hello @jmikola, Can you look at it, pls? |
I meant is you have inconsistent date format for updated_at String and Date might as well create a new field modified_at for custom updated_at to have better consistency. Saving everything as UtcDate is better and easy converting to different timezones. If you have need to convert some updated_at into AtomString and you have other fields that tracks it like is_date_string Example protected function updatedAt(): Attribute I will share to you that inconsistent updated_at type in database like String and Date it will fail when sorting and doing filters your string will get ignored when filtering date I mean it can be done in aggregate but you need to query all,.transform them to all dates and its going to suffer performance |
Thanks for opening a PR, we really appreciate people who take the time to dig out the bugs and come up with fixes. When you fix a bug, we require a non-regression test to be added. This is not the case here, which makes it hard to understand the bug despite the description.
In Laravel, it's possible to force a different value for the laravel-mongodb/src/Query/Builder.php Lines 792 to 802 in 2724144
If you remove the - ->update(['$set' => ['...' => '...', 'updated_at' => Carbon::now()->toAtomString()]]);
+ ->update(['...' => '...', 'updated_at' => Carbon::now()->toAtomString()]); You can update the PR to fix this issue: check if
MongoDB has a native date format, storing dates in strings is inefficient and reduces database capacity. The |
The $dateFormat is ignored when created_at and updated_at are added automatically (when model is saving) and every time set UTCDateTime as format for value in DB I think they ignored it because mongodb save date in Date type in the document itself. The BSON Date type allows for efficient and accurate date and time operations such as sorting, filtering (e.g., $gt, $lt), and aggregation framework operations (e.g., $dateToString, $dayOfMonth).BSON Date type ensures consistent handling and allows client applications to display dates in the desired time zone and index better Why storing dates as strings is generally discouraged: Inconsistent Formatting: Inefficient Operations: Performing date-based operations on string fields can be less efficient as it often requires string comparisons rather than native date comparisons. Time Zone Challenges: Handling time zones with string-based dates can be complex and error-prone, potentially leading to incorrect interpretations of time. |
Hello @GromNaN, It's just example of
MyModel::query()
->where('foo', 'bar')
->update(
[
'$set' => [
'...' => '...',
'updated_at' => Carbon::now()->toAtomString()
],
'$unset' => [
'....'
],
'$push' => [
'....'
],
'$pull' => [
'....'
],
]
); And I saw in source code even for this example possible to use: MyModel::query()
->where('foo', 'bar')
->update(
[
'...' => '...',
'updated_at' => Carbon::now()->toAtomString(),
'$unset' => [
'....'
],
'$push' => [
'....'
],
'$pull' => [
'....'
],
]
); But you must admit that the first option is more readable than the second 🙂
It's clear, but MongoDB is giving us freedom to choose what and how we would like to save data in our storage and just we responsible of it. If I consciously want to save data in a certain format, why not give me that choice? |
$set These raw operators already have a dedicated query builder methods which is more in line with laravel way of things. You can check the docs. |
@masterbater Ofc, has.
How it's possible to use those operators trough queue builder in one query? I don't see ability to run unset()->update()->pull()->push(); UPD: |
I feel you man, when I started using this package, I keep having issues and understanding do my biggest concern was that package compatibility and the native objectid relationship is not supported. There are times I wanted to fight and override the behavior but I just followed it (string relationships) and @GromNaN is working hard like the package compatibility using DocumentModel trait and the id attribute standardization in v5 was his greatest feature yet that makes it not so painful working with this package. And awesome work with @paulinevos is working on ide experience by adding types. @alcaeus and @jmikola they carry the back of the mongodb driver and do code review here. Hopefully this people would have more strength and dedication to continue improving this package as much as possible remove those pain points that is out of laravels query builder and eloquent compat |
@masterbater I see here has done big job. It's really flexible for me. As you mentioned - I can use independently:
And at the same time I can use all those methods in one ->update(
[
'$set' => [
'....'
],
'$unset' => [
'....'
],
'$push' => [
'....'
],
'$pull' => [
'....'
],
]
); Really nice they left this ability / behaviour - it's flexible. Not problem for me if someone will close this PR if it's not respond current paradigm. I've found unexpected behaviour and suggested fix that's all 🙂 |
Do you want to fix the issue that way?
|
@GromNaN this part not so understand... I'm checking it before
yes, I'll add tests. |
😂 sorry bro, it really has an unexpected behavior. Cheers on your PR bro and sorry for contributing noise here 🫶 |
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.
Your first change is indeed correct.
MongoDB is giving us freedom to choose what and how we would like to save data in our storage and just we responsible of it. If I consciously want to save data in a certain format, why not give me that choice?
That's conventions. We try to keep the choice when possible. Laravel is opinionated, for full flexibility you must use raw queries or the MongoDB library directly.
@GromNaN done, |
updated_at
when $set
is used. Fix ignoring dateFromat
updated_at
with $set
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.
Thank you @guram-vashakidze
@GromNaN thank you too 🤝 |
Hello,
updated_at
into$set
in Eloquent update. For example:In this case current implementation is overwrite
updated_at
$dateFormat
is ignored whencreated_at
andupdated_at
are added automatically (when model is saving) and every time setUTCDateTime
as format for value in DB. Added check - ifdateFormat
is set in model -> Date::now() is returned fromfreshTimestamp
instead ofUTCDateTime(Date::now())