codeigniter4 / codeigniter4/CodeIgniter4

Bug: Database dateFormat['datetime'] does not support millisecond or microsecond values

オープン
#10,540 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
bug
主要言語
PHP
スター
6k
フォーク
2k
平均マージ
1日 11時間
マージ済み PR(30日)
73

説明

### PHP Version

8.4

### CodeIgniter4 Version

4.7.4

### CodeIgniter4 Installation Method

Composer (as dependency to an existing project)

### Which operating systems have you tested for this bug?

Windows

### Which server did you use?

cli-server (PHP built-in webserver)

### Environment

development

### Database

MySQL 8.4.9

### What happened?

When setting the database connection in app\Config\Database.php, I am using the following dateFormat definition to get millisecond precision in my dates:
```
'dateFormat' => [
'date' => 'Y-m-d',
'datetime' => 'Y-m-d H:i:s.v',
'time' => 'H:i:s',
],
```
My model then configures the following for timestamps:
```
protected $useTimestamps = true;
protected $dateFormat = 'datetime';
protected $createdField = 'created_at';
```
After inserting an entry the table, the `created_at` column does not include the milliseconds. For example, the column has the value **2026-09-11 10:17:35** however because the column definition in MySQL is timestamp(6) the column should have the value **2026-09-11 10:17:35.509000**.

### Steps to Reproduce

Set the datetime mask as above and the model variables as above and insert a value into your table.

### Expected Output

I expected the `created_at` column to include the milliseconds.

### Anything else?

I have resolved this problem by overriding the `setDate()` function in the BaseModel as follows.

My model class is defined as:
```
class TransactionHistoryModel extends \App\Models\BaseModel {
...
}
```
I have then created my own BaseModel with the setDate override defined as:
```
namespace App\Models;

class BaseModel extends Model {

/**
* Sets the timestamp or current timestamp if null value is passed.
*
* @param int|null $userDate An optional PHP timestamp to be converted
*
* @return int|string
*
* @throws ModelException
*/
protected function setDate(?int $userDate = null)
{
$currentDate = $userDate ? Time::createFromTimestamp($userDate) : Time::now();

return $this->timeToDate($currentDate);
}
}
```
This solution works.

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

app\Config\Database.php の dateFormat 設定から始め、モデルの setDate() と timeToDate() のエントリポイントを追跡します。timestamp(6) カラムと報告された datetime マスクを使用して、MySQL 上でタイムスタンプ付きの insert を再現します。created_at が設定されたミリ秒またはマイクロ秒の精度を保持すれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
mysql, php
領域
backend, databases
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
活発
明瞭さ
おおむね明確
初心者へのやさしさ
72/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。