-
Notifications
You must be signed in to change notification settings - Fork 2k
Description
PHP Version
8.2, 8.4
CodeIgniter4 Version
4.6.1
CodeIgniter4 Installation Method
Composer (using codeigniter4/appstarter)
Which operating systems have you tested for this bug?
Linux
Which server did you use?
apache
Database
MariaDB
What happened?
When attempting to cast a database column of type DATE using the 'date' key in the model's $casts array, the system fails with an I18nException because the internal DatetimeCast handler attempts to use the long 'Y-m-d H:i:s' format.
This behavior prevents the correct casting of DATE fields to Time objects when the standard 'date' cast is used.
Steps to Reproduce
Create a model with a column named start_date that is defined as DATE in the database.
Set the cast for this column to 'date':
PHP
protected array $casts = [
// ... other fields ...
'start_date' => 'date',
];
Attempt to retrieve a record using the model (e.g., $model->find(1)).
Expected Result: The start_date field is successfully cast to a CodeIgniter\I18n\Time object, using the 'Y-m-d' format defined by $db->dateFormat['date'].
Actual Result: An I18nException is thrown: "Y-m-d H:i:s" is not a valid datetime format.
Expected Output
Extend getDateTimeFormat() in CodeIgniter\DataCaster\Cast\DatetimeCast
to include the 'd' parameter, for example:
PHP
protected static function getDateTimeFormat(array $params, BaseConnection $db): string
{
return match ($params[0] ?? '') {
'' => $db->dateFormat['datetime'],
'ms' => $db->dateFormat['datetime-ms'],
'us' => $db->dateFormat['datetime-us'],
'd' => $db->dateFormat['date'],
default => throw new InvalidArgumentException('Invalid parameter: ' . $params[0]),
};
}
PHP
and use in model
PHP
protected array $casts = [
'start_date' => 'datetime[d]',
'end_date' => 'datetime[d]',
PHP
The 'date' key in Config\Database::$dateFormat is documented in the user guide,
so it seems this should be supported for consistency.
Anything else?
Thanks for your great work on CI4!
Best regards,
Yevhen