thinkphp5 autoWriteTimestamp 时间戳

autoWriteTimestamp 时间戳类似原生的insertGetId和update方法中无效,只能用于save方法

第一种方式,是在数据库配置文件中添加全局设置:

// 开启自动写入时间戳字段
'auto_timestamp' => true,

第二种是直接在单独的模型类里面设置:

protected $autoWriteTimestamp = true;

如果这两个地方设置为true,默认识别为整型int类型,如果你的时间字段不是int类型的话,例如使用datetime类型的话,可以这样设置:

// 开启自动写入时间戳字段
'auto_timestamp' => 'datetime',

或者

protected $autoWriteTimestamp = 'datetime';

字段名默认创建时间字段为create_time,更新时间字段为update_time,支持的字段类型包括timestamp/datetime/int。

写入数据的时候,系统会自动写入create_time和update_time字段,而不需要定义修改器