top-think / think-orm

Think ORM——the PHP Database&ORM Framework
Apache License 2.0
413 stars 171 forks source link

更新数据时,创建时间字段create_time 未自动转换 #532

Closed efarsoft closed 3 months ago

efarsoft commented 4 months ago

更新数据时,创建时间字段create_time 未自动转换

已开启全局自动写入时间戳 image

提交数据 {"id":2,"work_type":1,"resume_id":2,"city":"12312312","post_id":0,"post_name":"3123123","salary_min":0,"salary_max":0,"salary_scope":"123123123","preference":"","industry":"123123123","create_time":"2024-05-11 16:01:55","update_time":"2024-05-11 16:01:55"} 请求返回 image

big-dream commented 4 months ago

表结构发下

efarsoft commented 3 months ago

image CREATE TABLE gxcc_social_category ( id int unsigned NOT NULL AUTO_INCREMENT COMMENT '自增ID', pid int unsigned NOT NULL DEFAULT '0' COMMENT '上级ID', level int NOT NULL DEFAULT '0' COMMENT '菜单级别', name varchar(60) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '版块名称', image varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '缩略图', description varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '版块简介', sort smallint unsigned NOT NULL DEFAULT '255' COMMENT '排序(ASC)', status tinyint unsigned NOT NULL DEFAULT '1' COMMENT '状态值', is_section tinyint unsigned NOT NULL DEFAULT '0' COMMENT '是否设置为版块', is_recommend tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否推荐', cross_plfm tinyint unsigned NOT NULL DEFAULT '0', cross_plfm_app_id int DEFAULT NULL COMMENT '跨平台ID', cross_plfm_category_id int DEFAULT NULL COMMENT '分类id', link_url varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '跳转链接', app_id int NOT NULL COMMENT '应用ID', create_time int NOT NULL COMMENT '创建时间', update_time int NOT NULL COMMENT '更新时间', delete_time int DEFAULT NULL COMMENT '删除时间', PRIMARY KEY (id) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=252 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

yaobiao131 commented 3 months ago

同样的问题

big-dream commented 3 months ago

自动写入时间戳字段仅会处理系统自动生成的值,你自己传入的 update_timecreate_time 不会处理。 自动写入时间戳字段输出的时候会自动进行格式转换,写入时不会自动进行格式转换。 如果你需要对自己传入的 update_timecreate_time 自动进行格式转换,以下提出两种方案:

1.设置字段类型

文档:https://doc.thinkphp.cn/v8_0/type_conversion.html

class GxccSocialCategory extends Model
{
    protected $type = [
        'create_time' => 'timestamp',
        'update_time' => 'timestamp',
    ];
}

2.设置修改器

文档:https://doc.thinkphp.cn/v8_0/model_setter.html

class GxccSocialCategory extends Model
{
    public function setCreateTimeAttr($value)
    {
        return strtotime($value);
    }

    public function setUpdateTimeAttr($value)
    {
        return strtotime($value);
    }
}