Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -1425,9 +1425,11 @@ protected function doProtectFieldsForInsert(array $row): array
*/
protected function setDate(?int $userDate = null)
{
$currentDate = $userDate ?? Time::now()->getTimestamp();
if ($userDate === null && $this->dateFormat === 'datetime') {
return $this->timeToDate(Time::now());
}

return $this->intToDate($currentDate);
return $this->intToDate($userDate ?? Time::now()->getTimestamp());
}

/**
Expand Down
26 changes: 26 additions & 0 deletions tests/system/Models/GeneralModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@

use CodeIgniter\Database\BaseConnection;
use CodeIgniter\Exceptions\BadMethodCallException;
use CodeIgniter\I18n\Time;
use CodeIgniter\Model;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockConnection;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use Tests\Support\Models\JobModel;
use Tests\Support\Models\UserModel;
Expand All @@ -39,6 +42,7 @@ protected function tearDown(): void
{
parent::tearDown();
$this->resetServices();
Time::setTestNow();
}

/**
Expand Down Expand Up @@ -111,6 +115,28 @@ public function testSetAllowedFields(): void
$this->assertSame($allowed2, $this->getPrivateProperty($model, 'allowedFields'));
}

#[DataProvider('provideCurrentTimestampPreservesSubseconds')]
public function testCurrentTimestampPreservesSubseconds(string $format, string $expected): void
{
Time::setTestNow('2024-07-09 09:13:34.654321');

$db = new MockConnection(['dateFormat' => ['datetime' => $format]]);
$model = $this->createModel(UserModel::class, $db);
$date = self::getPrivateMethodInvoker($model, 'setDate');

$this->assertSame($expected, $date());
}

/**
* @return iterable<string, array{string, string}>
*/
public static function provideCurrentTimestampPreservesSubseconds(): iterable
{
yield 'milliseconds' => ['Y-m-d H:i:s.v', '2024-07-09 09:13:34.654'];

yield 'microseconds' => ['Y-m-d H:i:s.u', '2024-07-09 09:13:34.654321'];
}

public function testBuilderUsesModelTable(): void
{
$builder = $this->createModel(UserModel::class)->builder();
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.7.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Bugs Fixed
**********

- **Autoloader:** Fixed a bug where ``FileLocatorCached::deleteCache()`` left the deleted data in memory, so it could be written back to the cache file on shutdown. ``spark optimize`` and ``spark cache:clear`` now clear the shared locator's cache instead of a separate instance.
- **BaseModel:** Fixed a bug where auto-generated ``created_at``/``updated_at`` timestamps always rendered ``.000000`` for a ``'datetime'`` ``$dateFormat`` whose connection ``dateFormat`` mask includes ``.v``/``.u``, instead of the real sub-second value.
- **CLI:** Fixed a bug where pressing backspace in a ``CLI::prompt()`` erased the prompt text when the ``readline`` extension is enabled. The prompt is now passed to ``readline()`` so line redraws repaint it.
ANSI color codes in the prompt (e.g., option defaults) are wrapped in readline's non-printing markers under GNU readline so cursor positioning stays accurate.
On Windows, where the ``readline`` extension is built on WinEditLine, the prompt is written to STDOUT first because WinEditLine reports no library version and prints ANSI sequences literally.
Expand Down
Loading