Don't call callbacks if has error

This commit is contained in:
Jinzhu 2015-02-24 16:28:15 +08:00
parent 5586d04999
commit 83ee11e184
5 changed files with 17 additions and 14 deletions

View File

@ -6,8 +6,8 @@ import (
) )
func BeforeCreate(scope *Scope) { func BeforeCreate(scope *Scope) {
scope.CallMethod("BeforeSave") scope.CallMethodWithErrorCheck("BeforeSave")
scope.CallMethod("BeforeCreate") scope.CallMethodWithErrorCheck("BeforeCreate")
} }
func UpdateTimeStampWhenCreate(scope *Scope) { func UpdateTimeStampWhenCreate(scope *Scope) {
@ -78,8 +78,8 @@ func Create(scope *Scope) {
} }
func AfterCreate(scope *Scope) { func AfterCreate(scope *Scope) {
scope.CallMethod("AfterCreate") scope.CallMethodWithErrorCheck("AfterCreate")
scope.CallMethod("AfterSave") scope.CallMethodWithErrorCheck("AfterSave")
} }
func init() { func init() {

View File

@ -3,7 +3,7 @@ package gorm
import "fmt" import "fmt"
func BeforeDelete(scope *Scope) { func BeforeDelete(scope *Scope) {
scope.CallMethod("BeforeDelete") scope.CallMethodWithErrorCheck("BeforeDelete")
} }
func Delete(scope *Scope) { func Delete(scope *Scope) {
@ -24,7 +24,7 @@ func Delete(scope *Scope) {
} }
func AfterDelete(scope *Scope) { func AfterDelete(scope *Scope) {
scope.CallMethod("AfterDelete") scope.CallMethodWithErrorCheck("AfterDelete")
} }
func init() { func init() {

View File

@ -103,7 +103,7 @@ func Query(scope *Scope) {
} }
func AfterQuery(scope *Scope) { func AfterQuery(scope *Scope) {
scope.CallMethod("AfterFind") scope.CallMethodWithErrorCheck("AfterFind")
} }
func init() { func init() {

View File

@ -26,8 +26,8 @@ func AssignUpdateAttributes(scope *Scope) {
func BeforeUpdate(scope *Scope) { func BeforeUpdate(scope *Scope) {
if _, ok := scope.Get("gorm:update_column"); !ok { if _, ok := scope.Get("gorm:update_column"); !ok {
scope.CallMethod("BeforeSave") scope.CallMethodWithErrorCheck("BeforeSave")
scope.CallMethod("BeforeUpdate") scope.CallMethodWithErrorCheck("BeforeUpdate")
} }
} }
@ -67,8 +67,8 @@ func Update(scope *Scope) {
func AfterUpdate(scope *Scope) { func AfterUpdate(scope *Scope) {
if _, ok := scope.Get("gorm:update_column"); !ok { if _, ok := scope.Get("gorm:update_column"); !ok {
scope.CallMethod("AfterUpdate") scope.CallMethodWithErrorCheck("AfterUpdate")
scope.CallMethod("AfterSave") scope.CallMethodWithErrorCheck("AfterSave")
} }
} }

View File

@ -154,9 +154,8 @@ func (scope *Scope) SetColumn(column interface{}, value interface{}) error {
return errors.New("could not convert column to field") return errors.New("could not convert column to field")
} }
// CallMethod invoke method with necessary argument func (scope *Scope) CallMethod(name string, checkError bool) {
func (scope *Scope) CallMethod(name string) { if scope.Value == nil && (!checkError || !scope.HasError()) {
if scope.Value == nil {
return return
} }
@ -190,6 +189,10 @@ func (scope *Scope) CallMethod(name string) {
} }
} }
func (scope *Scope) CallMethodWithErrorCheck(name string) {
scope.CallMethod(name, true)
}
// AddToVars add value as sql's vars, gorm will escape them // AddToVars add value as sql's vars, gorm will escape them
func (scope *Scope) AddToVars(value interface{}) string { func (scope *Scope) AddToVars(value interface{}) string {
scope.SqlVars = append(scope.SqlVars, value) scope.SqlVars = append(scope.SqlVars, value)