Skip to content

Bug: Model does not update created_at column #8247

@kenjis

Description

@kenjis

PHP Version

8.1

CodeIgniter4 Version

4.4.3 and develop 1d6b7ac

CodeIgniter4 Installation Method

Git

Which operating systems have you tested for this bug?

macOS

Which server did you use?

cli

Database

n/a

What happened?

The following test fails:

<?php

namespace CodeIgniter\Models;

use Tests\Support\Models\UserTimestampModel;

/**
 * @group DatabaseLive
 *
 * @internal
 */
final class TimestampModelTest extends LiveModelTestCase
{
    protected $migrate     = true;
    protected $migrateOnce = true;
    protected $refresh     = false;
    protected $seed        = '';

    private function prepareOneRecord(): void
    {
        $this->createModel(UserTimestampModel::class);
        $this->db->table('user')->truncate();

        $data = [
            'name'       => 'John Smith',
            'email'      => 'john@example.com',
            'country'    => 'US',
            'created_at' => '2000-01-01 12:00:00',
            'updated_at' => '2000-01-01 12:00:00',
        ];
        $this->model->insert($data);
    }

    public function testUpdateArrayUpdates UpdatedAt(): void
    {
        $this->prepareOneRecord();

        $user = $this->model->find(1);

        $user['country'] = 'CA';
        $this->model->update($user['id'], $user);

        $user = $this->model->find(1);

        $this->assertNotSame('', $user['updated_at']);
        $this->assertNotSame($user['created_at'], $user['updated_at']);
    }
}
<?php

namespace Tests\Support\Models;

use CodeIgniter\Model;

class UserTimestampModel extends Model
{
    protected $table         = 'user';
    protected $allowedFields = [
        'name',
        'email',
        'country',
        'created_at',
        'updated_at',
        'deleted_at',
    ];
    protected $returnType     = 'array';
    protected $useSoftDeletes = true;
    protected $useTimestamps  = true;
    protected $dateFormat     = 'datetime';
}

Steps to Reproduce

Run test above.

Expected Output

The test passes.

Anything else?

$useTimestamps
This boolean value determines whether the current date is automatically added to all inserts and updates. If true, will set the current time in the format specified by $dateFormat.
https://codeigniter4.github.io/CodeIgniter4/models/model.html#dates

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions