Documentation
¶
Index ¶
- func EnsureSelects(tx *gorm.DB, fieldNames ...string)
- func SetFieldIfExist(model interface{}, fieldName string, value string)
- type MysqlBaseModel
- func (b *MysqlBaseModel) BeforeCreate(tx *gorm.DB) (err error)
- func (b *MysqlBaseModel) BeforeUpdate(tx *gorm.DB) (err error)
- func (b *MysqlBaseModel) GetConfigAlias(model interface{}) string
- func (b *MysqlBaseModel) ModelParse(modelType reflect.Type) (tableName string, fields []string, softDeleteField string, ...)
- type SqliteBaseModel
- func (b *SqliteBaseModel) BeforeCreate(tx *gorm.DB) (err error)
- func (b *SqliteBaseModel) BeforeUpdate(tx *gorm.DB) (err error)
- func (b *SqliteBaseModel) GetConfigAlias(model interface{}) string
- func (b *SqliteBaseModel) ModelParse(modelType reflect.Type) (tableName string, fields []string, softDeleteField string, ...)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EnsureSelects ¶ added in v0.13.10
EnsureSelects 在使用 Select 限定插入/更新字段时,确保必要字段被包含 典型场景:在钩子中设置 CreatedAt/UpdatedAt 等字段,但外部使用了 Select 导致未持久化 函数名:EnsureSelects 参数:
- tx *gorm.DB:当前事务上下文
- fieldNames ...string:结构体字段名列表(如 "UpdatedAt")
返回值:无(直接修改 tx.Statement.Selects) 异常:无(只在 Schema 可用时生效) 使用示例:
EnsureSelects(tx, "CreatedAt", "UpdatedAt")
func SetFieldIfExist ¶ added in v0.10.4
SetFieldIfExist 设置字段值(如果字段存在且可设置) 支持字符串类型和 time.Time 类型字段的自动设置 函数名:SetFieldIfExist 参数:
- model interface{}:模型对象或其指针
- fieldName string:结构体字段名(非列名)
- value string:用于设置的字符串值(时间格式:2006-01-02 15:04:05)
返回值:无 异常:无(内部忽略不可设或类型不匹配的字段) 使用示例:
SetFieldIfExist(&user, "UpdatedAt", "2025-01-01 00:00:00")
Types ¶
type MysqlBaseModel ¶
type MysqlBaseModel struct {
}
MysqlBaseModel gorm基础模型
func (*MysqlBaseModel) BeforeCreate ¶
func (b *MysqlBaseModel) BeforeCreate(tx *gorm.DB) (err error)
BeforeCreate 创建前钩子 函数名:BeforeCreate 参数:
- tx *gorm.DB:当前事务上下文
返回值:
- err error:错误信息(当前不返回错误)
说明:设置 CreatedAt/UpdatedAt 等时间字段;在使用 Select 限定字段时,确保时间字段包含在持久化列表中 使用示例:
db.Create(&model) // 自动触发
func (*MysqlBaseModel) BeforeUpdate ¶
func (b *MysqlBaseModel) BeforeUpdate(tx *gorm.DB) (err error)
BeforeUpdate 更新前钩子 函数名:BeforeUpdate 参数:
- tx *gorm.DB:当前事务上下文
返回值:
- err error:错误信息(当前不返回错误)
说明:更新 UpdatedAt/Updated 时间字段;在使用 Select 限定字段时,确保时间字段包含在持久化列表中 使用示例:
db.Model(&model).Updates(map[string]interface{}{...}) // 自动触发
func (*MysqlBaseModel) GetConfigAlias ¶
func (b *MysqlBaseModel) GetConfigAlias(model interface{}) string
func (*MysqlBaseModel) ModelParse ¶
func (b *MysqlBaseModel) ModelParse(modelType reflect.Type) (tableName string, fields []string, softDeleteField string, softDeleteCondition string)
ModelParse 解析模型信息(供 CRUD trait 使用) 参数说明:
- modelType reflect.Type: 模型的反射类型
返回值:
- tableName string: 数据表名称
- fields []string: 字段列表
- softDeleteField string: 软删除字段名
- softDeleteCondition string: 软删除条件
type SqliteBaseModel ¶
type SqliteBaseModel struct {
}
SqliteBaseModel gorm基础模型
func (*SqliteBaseModel) BeforeCreate ¶
func (b *SqliteBaseModel) BeforeCreate(tx *gorm.DB) (err error)
BeforeCreate 创建前钩子 函数名:BeforeCreate 参数:
- tx *gorm.DB:当前事务上下文
返回值:
- err error:错误信息(当前不返回错误)
说明:设置 CreatedAt/UpdatedAt 等时间字段;在使用 Select 限定字段时,确保时间字段包含在持久化列表中 使用示例:
db.Create(&model) // 自动触发
func (*SqliteBaseModel) BeforeUpdate ¶
func (b *SqliteBaseModel) BeforeUpdate(tx *gorm.DB) (err error)
BeforeUpdate 更新前钩子 函数名:BeforeUpdate 参数:
- tx *gorm.DB:当前事务上下文
返回值:
- err error:错误信息(当前不返回错误)
说明:更新 UpdatedAt/Updated 时间字段;在使用 Select 限定字段时,确保时间字段包含在持久化列表中 使用示例:
db.Model(&model).Updates(map[string]interface{}{...}) // 自动触发
func (*SqliteBaseModel) GetConfigAlias ¶ added in v0.11.1
func (b *SqliteBaseModel) GetConfigAlias(model interface{}) string
func (*SqliteBaseModel) ModelParse ¶
func (b *SqliteBaseModel) ModelParse(modelType reflect.Type) (tableName string, fields []string, softDeleteField string, softDeleteCondition string)
ModelParse 解析模型信息(供 CRUD trait 使用) 参数说明:
- modelType reflect.Type: 模型的反射类型
返回值:
- tableName string: 数据表名称
- fields []string: 字段列表
- softDeleteField string: 软删除字段名
- softDeleteCondition string: 软删除条件