首页 > 解决方案 > 在 codeigniter4 的模型中使用 $dateFormat INT

问题描述

阅读 CodeIgniter4 (Models) 的文档,我发现了这个。

在此处输入图像描述

我尝试使用日期类型为 int 的时间戳,但这不起作用,因为在更新列“updated_at”时它为 0。我的模型是:

<?php namespace App\Models;

use CodeIgniter\Model;

class UserModel extends eCrisisModel{
    
    protected $table = 'user';
    protected $primaryKey = 'id';
    
    protected $returnType = 'array';
    protected $useSoftDeletes = true;

    protected $allowedFields = ["login", "pass", "about_me", "utc"];

    protected $useTimestamps = true;
    protected $createdField = 'created_at';
    protected $updatedField = 'updated_at';
    protected $deletedField = 'deleted_at';
    protected $dateFormat = 'int';
}

?>

eCrisisModel 是 Model 类的抽象类扩展:

<?php namespace App\Models;

use CodeIgniter\Model;

// existentialCrisis
abstract class eCrisisModel extends Model{

    protected $prefix = '';
    protected $exists = false;
    protected $builder;
    public function __construct(...$params){
        parent::__construct(...$params);
        //"I am therefore I think"
        if(!$this->db->tableExists($this->prefix.$this->table)){
            $this->creatingMe();
            $this->exists = true;
        }
        $this->builder = $this->db->table($this->table);
    }

    public function creatingMe(){
        //...
    }
}

?>

执行它的控制器是:

<?php namespace App\Controllers;

use CodeIgniter\Controller;

class Test extends Controller{

    public function __construct(){
        $this->UserModel = model('App\Models\UserModel');
        $this->SettingModel = model('App\Models\SettingModel');
    }

    public function index(){
        $data = [
                'login' => 'jiji',
                'pass'  => password_hash("passss",PASSWORD_DEFAULT),
                'about_me'  => 'xd',
                'utc' => -2.75
        ];

        $this->UserModel->builder->where('id', 1);
        $this->UserModel->builder->update($data);
    }
}

?>

更新后的数据库是: 在此处输入图像描述

描述表格; 在此处输入图像描述

哪个是我的错误?为什么系统不记录更新时间戳?

标签: phpmodeltimestampcodeigniter-4

解决方案


推荐阅读