forked from mirror/gorm
make update column works with new plugin system
This commit is contained in:
parent
506d14a2f2
commit
048b8b6abe
|
@ -10,8 +10,12 @@ func AssignUpdateAttributes(scope *Scope) {
|
|||
if attrs, ok := scope.Get("gorm:update_interface"); ok {
|
||||
if maps := convertInterfaceToMap(attrs); len(maps) > 0 {
|
||||
protected, ok := scope.Get("gorm:ignore_protected_attrs")
|
||||
_, updateColumn := scope.Get("gorm:update_column")
|
||||
updateAttrs, hasUpdate := scope.updatedAttrsWithValues(maps, ok && protected.(bool))
|
||||
if len(updateAttrs) > 0 {
|
||||
|
||||
if updateColumn {
|
||||
scope.Set("gorm:update_attrs", maps)
|
||||
} else if len(updateAttrs) > 0 {
|
||||
scope.Set("gorm:update_attrs", updateAttrs)
|
||||
} else if !hasUpdate {
|
||||
scope.SkipLeft()
|
||||
|
@ -22,12 +26,16 @@ func AssignUpdateAttributes(scope *Scope) {
|
|||
}
|
||||
|
||||
func BeforeUpdate(scope *Scope) {
|
||||
scope.CallMethod("BeforeSave")
|
||||
scope.CallMethod("BeforeUpdate")
|
||||
_, ok := scope.Get("gorm:update_column")
|
||||
if !ok {
|
||||
scope.CallMethod("BeforeSave")
|
||||
scope.CallMethod("BeforeUpdate")
|
||||
}
|
||||
}
|
||||
|
||||
func UpdateTimeStampWhenUpdate(scope *Scope) {
|
||||
if !scope.HasError() {
|
||||
_, ok := scope.Get("gorm:update_column")
|
||||
if !ok {
|
||||
scope.SetColumn("UpdatedAt", time.Now())
|
||||
}
|
||||
}
|
||||
|
@ -62,8 +70,11 @@ func Update(scope *Scope) {
|
|||
}
|
||||
|
||||
func AfterUpdate(scope *Scope) {
|
||||
scope.CallMethod("AfterUpdate")
|
||||
scope.CallMethod("AfterSave")
|
||||
_, ok := scope.Get("gorm:update_column")
|
||||
if !ok {
|
||||
scope.CallMethod("AfterUpdate")
|
||||
scope.CallMethod("AfterSave")
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
|
9
main.go
9
main.go
|
@ -172,11 +172,14 @@ func (s *DB) Updates(values interface{}, ignoreProtectedAttrs ...bool) *DB {
|
|||
}
|
||||
|
||||
func (s *DB) UpdateColumn(attrs ...interface{}) *DB {
|
||||
return s.UpdateColumns(toSearchableMap(attrs...), true)
|
||||
return s.UpdateColumns(toSearchableMap(attrs...))
|
||||
}
|
||||
|
||||
func (s *DB) UpdateColumns(values interface{}, ignore_protected_attrs ...bool) *DB {
|
||||
return s.clone().do(s.Value).begin().updateColumns(values).commit_or_rollback().db
|
||||
func (s *DB) UpdateColumns(values interface{}) *DB {
|
||||
return s.clone().NewScope(s.Value).
|
||||
Set("gorm:update_interface", values).
|
||||
Set("gorm:update_column", true).
|
||||
callCallbacks(s.parent.callback.updates).db
|
||||
}
|
||||
|
||||
func (s *DB) Save(value interface{}) *DB {
|
||||
|
|
Loading…
Reference in New Issue