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) {
scope.CallMethod("BeforeSave")
scope.CallMethod("BeforeCreate")
scope.CallMethodWithErrorCheck("BeforeSave")
scope.CallMethodWithErrorCheck("BeforeCreate")
}
func UpdateTimeStampWhenCreate(scope *Scope) {
@ -78,8 +78,8 @@ func Create(scope *Scope) {
}
func AfterCreate(scope *Scope) {
scope.CallMethod("AfterCreate")
scope.CallMethod("AfterSave")
scope.CallMethodWithErrorCheck("AfterCreate")
scope.CallMethodWithErrorCheck("AfterSave")
}
func init() {

View File

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

View File

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

View File

@ -26,8 +26,8 @@ func AssignUpdateAttributes(scope *Scope) {
func BeforeUpdate(scope *Scope) {
if _, ok := scope.Get("gorm:update_column"); !ok {
scope.CallMethod("BeforeSave")
scope.CallMethod("BeforeUpdate")
scope.CallMethodWithErrorCheck("BeforeSave")
scope.CallMethodWithErrorCheck("BeforeUpdate")
}
}
@ -67,8 +67,8 @@ func Update(scope *Scope) {
func AfterUpdate(scope *Scope) {
if _, ok := scope.Get("gorm:update_column"); !ok {
scope.CallMethod("AfterUpdate")
scope.CallMethod("AfterSave")
scope.CallMethodWithErrorCheck("AfterUpdate")
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")
}
// CallMethod invoke method with necessary argument
func (scope *Scope) CallMethod(name string) {
if scope.Value == nil {
func (scope *Scope) CallMethod(name string, checkError bool) {
if scope.Value == nil && (!checkError || !scope.HasError()) {
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
func (scope *Scope) AddToVars(value interface{}) string {
scope.SqlVars = append(scope.SqlVars, value)