-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat: QueryBuilder insert/update support Time object #6456
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
eb7338e
to
5fe53ec
Compare
I'm going to reference my original comment just so it is included here. I'm glad to see that Deptrac agreed with me 🤗 |
What if I want the Time object to be converted to a unix timestamp? |
Pass the timestamp to QueryBuilder. Background:
|
In the same way, you can also pass a timestamp in ISO format to the QueryBuilder. |
It may be true, but in this case most developers cannot bring the data to the desired format because of the ignorance. Do you want to raise an Exception when any object is passed? Even if we do so, if a developer write code like I am not sure, but perhaps the design of Time object that changes behavior depending on the locale is a mistake. |
set('last_active', Time::now()->format()); // or ->toDatabase() To be honest, I don't know what it's for locale dependency to serve timestamps within the application. Except when it needs to be displayed to the user. |
Yes, every time you pass Time to QueryBuilder, you should call But I think it is difficult for human beings. set('last_active', $user->last_active) |
Indeed. It might be better to fix |
I created another PR #6461 to fix Time. |
I worry that will be a disruptive breaking change for anyone using a locale without decimal numbers. A third option (not without its own issues) we create |
My 2 cents... I don't think we should change anything about the current behavior. We have Also "ignorance of developers" is not a good advertisement for adding this feature. Like... it's not a framework fault when users don't know how to properly handle the date. Hopefully, they will learn when they see a query error.
|
It is very difficult to learn or notice it, because ignorant developers never see the query error. If we expect developers to handle this properly, we need to find the bug in codeigniter4/shield#392 in the review. |
Because the main cause of this problem has been identified in the locale |
The That is, for |
This may sound harsh, but... basically, it seems like we missed a "thing" and merged buggy code, and now we're trying to prevent it by changing the class behavior. Things like this are easy to catch if we use For me, everything comes down to good habits when working with dates in general. Although working with Entities and Models makes things no hassle, we have to be precise with QueryBuilder. My personal preference would be to keep it that way. |
It is not. But I think #6461 is better than this PR now. It does not change QueryBuilder at all. |
The setter does not help us in this case. |
During saving, the toRawArray() method is called, which returns the desired value. |
In this case, we need to avoid updating the |
I think @iRedds is correct. As we've been seeing with a lot of these recent Entity discussions it is important to keep the two concerns strict: persistent storage ("raw" This doesn't solve QueryBuilder if a developer calls |
@MGatner I also think it is bad design that the current Entity has Time object in $attributes.
No, BaseModel takes care of Time objects. |
I will add, since this came up organically, that I've been working on an alternate data layer concept that follows the repository pattern more closely and addresses a number of these issues that I think ultimately are architecture problems. Still very much a work in progress but I hope this will be some inspiration for our data access in v5: https://github.com/tattersoftware/codeigniter4-repositories One of the fundamental differences (which comes straight from the repository pattern) is that an Entity is a domain object and has no concerns about persistence except at the layer boundary where it becomes a repository-specific data-transfer object (array, in my case). Practically this means: Entity values are always "for the developer" and thus favor value objects (self-validating) and cast types, and any persistence concerns happen in |
Oh right! I forgot about that one. Still a concern though. |
a35d1bc
to
7ef8257
Compare
I would like to proceed #6461 instead of this. |
I agree, that is a better solution. |
I thought of this. It would be nice if a select query could return date time objects as well. I though of creating a wrapper class around date/time object. Have __toString format according to database and have configuration setting to format for front end. |
#6461 was merged. |
Description
Closes #6439
Now you can use Time in all locales.
Background:
Time
is stringable.Time
, it causes a query error.Time
is a standard class in CI4.Checklist: