-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
[Model] DateTime hardcoded format Y-m-d H:i:s
drives to error on some databases
#7177
Comments
Personally, I think it is a bad practice to change the date/time format in a program depending on the locale. However, if there is a database that does not accept |
A possible solution would be to allow a transiction when inserting/updating from inside the Model (so we can call If you would agree, I could go ahead and make a PR changing the logic behind the hardcoded format inside BaseModel, DeleteModelTest and FindModelTest (these are the classes I was able to notice that work with databases and dates). In case, if you think of anything else, please let me know. |
It is not efficient. @codeigniter4/database-team |
Sounds like this is a model "problem", so I would make a change there... if we really want to make this happen. Now, we can choose the format from protected $dateFormat = 'datetime[d-m-Y H:i:s]'; It would be optional. No parameter would mean a standard format (Y-m-d H:i:s). Although I don't know whether to expect the same from |
Checks if it contains only |
But after all, we need the properties for the format strings (datetime and date)? |
I think I would simplify it. We have protected $dateFormat = 'd-m-Y H:i:s'; And that's it. We can use But changing |
A parallel issue that is unrelated to locale... There are times where one will want to store microseconds. A global setting would work. A model-level setting would be nice. A column-level setting would be really nice. Ideally with a cascading defaults (i.e. if column-level is not set, use model-level; if model-level is not set, use global setting). |
I hate how infrastructure-dependent our Model class is. It would be good to start a new solution for this, rather than continuing to massage Model/BaseModel to work with any database. What if Model just sticks with using |
This issue is, after all, a database date/time format config. So, wouldn't it be a good idea to add Even if we set the date/time format in the Model property, we cannot change it in a library like Shield, because there is no configuration file for Models. |
It seems that this setting is only related to the model, so adding this setting to the database configuration doesn't seem right to me. I would rather see an additional |
We could use this new config to handle other things in a model, like: #8455 |
Yes, so far there does not seem to be any place to use the date format setting other than in Model. |
I thought again. Currently, this date/time format is only relevant for Model. However, it is essentially an attribute of a database connection. If you are using two database connections, one with An implementation where the database connection has a date/time format and the model uses it seems correct. |
It is fine that Model just use only Time. |
At that point it would need to be a concern of the database driver. For example, in Builder any time it added something to class MySqlTime extends Time
{
protected string $toStringFormat = 'Y-m-d H:i:s';
} This bleeds the underlying driver out into userland code though, because setting or modifying that field requires knowing its type. Honestly: we're cheating right now. We use a |
In the following layers, I think this datetime format belongs to Connection.
|
I agree, as far as a configurable solution goes. Dates/times are a unique because a) all databases (AFAIK) have a native scalar type for them, while PHP does not, and b) they are super common (unlike, e.g. spatial data). I do wonder if There's one other assumption in the translation we should be mindful of, that |
Y-m-d H:i:s
drives to error on some databases
Y-m-d H:i:s
drives to error on some databasesY-m-d H:i:s
drives to error on some databases
I sent PR #8544 for a bug fix regarding time zones. |
@mikeabbott10 #8538 has been merged into |
This issue should be fixed in v4.5.1. |
PHP Version
8.1
CodeIgniter4 Version
4.3.0
CodeIgniter4 Installation Method
Manual (zip or tar.gz)
Which operating systems have you tested for this bug?
Windows
Which server did you use?
apache
Database
SQL Server 2019
What happened?
My db expects datetime format like
'd-m-Y H:i:s'
. Unfortunately, the format'Y-m-d H:i:s'
is hardcoded in several parts of the CI code and trying to insert something into a table with, for example,'created_at'
column drives me to varchar-to-datetime conversion error.Steps to Reproduce
Extend CodeIgniter\Model and try to save something to a table with
'created_at'
(or'updated_at'
) column, without explicitly set that value (doing so leaves BaseModel the task to handle the'created_at'
column value). The table is hosted in a db with DMY dateformat.Expected Output
The insertion should work
Anything else?
The discussion started here. Initially, I thought it was only a CodeIgniter Shield problem, but it's not.
The text was updated successfully, but these errors were encountered: