diff --git a/callback_update.go b/callback_update.go index 5b1ff338..09c12508 100644 --- a/callback_update.go +++ b/callback_update.go @@ -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() { diff --git a/main.go b/main.go index e0a44caf..c9135eba 100644 --- a/main.go +++ b/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 { diff --git a/scope.go b/scope.go index 5401556c..5ebcde98 100644 --- a/scope.go +++ b/scope.go @@ -65,6 +65,10 @@ func (scope *Scope) Err(err error) error { return err } +func (scope *Scope) Log(v ...interface{}) { + scope.db.log(v...) +} + func (scope *Scope) HasError() bool { return scope.db.hasError() }