mirror of
https://gitee.com/johng/gf
synced 2026-07-06 13:42:46 +08:00
improve hook feature for package gdb
This commit is contained in:
@ -77,7 +77,7 @@ type HookInsertInput struct {
|
||||
type HookUpdateInput struct {
|
||||
internalParamHookUpdate
|
||||
Table string
|
||||
Data interface{}
|
||||
Data interface{} // Data can be type of: map[string]interface{}/string. You can use type assertion on `Data`.
|
||||
Condition string
|
||||
Args []interface{}
|
||||
}
|
||||
|
||||
@ -44,27 +44,28 @@ func (m *Model) Update(dataAndWhere ...interface{}) (result sql.Result, err erro
|
||||
}
|
||||
var (
|
||||
updateData = m.data
|
||||
reflectInfo = reflection.OriginTypeAndKind(updateData)
|
||||
fieldNameUpdate = m.getSoftFieldNameUpdated()
|
||||
conditionWhere, conditionExtra, conditionArgs = m.formatCondition(false, false)
|
||||
)
|
||||
// Automatically update the record updating time.
|
||||
if !m.unscoped && fieldNameUpdate != "" {
|
||||
reflectInfo := reflection.OriginTypeAndKind(m.data)
|
||||
switch reflectInfo.OriginKind {
|
||||
case reflect.Map, reflect.Struct:
|
||||
dataMap := m.db.ConvertDataForRecord(m.GetCtx(), m.data)
|
||||
if fieldNameUpdate != "" {
|
||||
dataMap[fieldNameUpdate] = gtime.Now().String()
|
||||
}
|
||||
updateData = dataMap
|
||||
switch reflectInfo.OriginKind {
|
||||
case reflect.Map, reflect.Struct:
|
||||
dataMap := m.db.ConvertDataForRecord(m.GetCtx(), m.data)
|
||||
// Automatically update the record updating time.
|
||||
if !m.unscoped && fieldNameUpdate != "" {
|
||||
dataMap[fieldNameUpdate] = gtime.Now().String()
|
||||
}
|
||||
updateData = dataMap
|
||||
|
||||
default:
|
||||
updates := gconv.String(m.data)
|
||||
default:
|
||||
updates := gconv.String(m.data)
|
||||
// Automatically update the record updating time.
|
||||
if !m.unscoped && fieldNameUpdate != "" {
|
||||
if fieldNameUpdate != "" && !gstr.Contains(updates, fieldNameUpdate) {
|
||||
updates += fmt.Sprintf(`,%s='%s'`, fieldNameUpdate, gtime.Now().String())
|
||||
}
|
||||
updateData = updates
|
||||
}
|
||||
updateData = updates
|
||||
}
|
||||
newData, err := m.filterDataForInsertOrUpdate(updateData)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user