mirror of https://github.com/go-gorm/gorm.git
Fix hooks order, close https://github.com/go-gorm/gorm.io/pull/519
This commit is contained in:
parent
d66f37ad32
commit
a7b3b5956f
|
@ -10,6 +10,7 @@ import (
|
|||
"gorm.io/gorm/utils"
|
||||
)
|
||||
|
||||
// BeforeCreate before create hooks
|
||||
func BeforeCreate(db *gorm.DB) {
|
||||
if db.Error == nil && db.Statement.Schema != nil && !db.Statement.SkipHooks && (db.Statement.Schema.BeforeSave || db.Statement.Schema.BeforeCreate) {
|
||||
callMethod(db, func(value interface{}, tx *gorm.DB) (called bool) {
|
||||
|
@ -31,6 +32,7 @@ func BeforeCreate(db *gorm.DB) {
|
|||
}
|
||||
}
|
||||
|
||||
// Create create hook
|
||||
func Create(config *Config) func(db *gorm.DB) {
|
||||
supportReturning := utils.Contains(config.CreateClauses, "RETURNING")
|
||||
|
||||
|
@ -146,22 +148,23 @@ func Create(config *Config) func(db *gorm.DB) {
|
|||
}
|
||||
}
|
||||
|
||||
// AfterCreate after create hooks
|
||||
func AfterCreate(db *gorm.DB) {
|
||||
if db.Error == nil && db.Statement.Schema != nil && !db.Statement.SkipHooks && (db.Statement.Schema.AfterSave || db.Statement.Schema.AfterCreate) {
|
||||
callMethod(db, func(value interface{}, tx *gorm.DB) (called bool) {
|
||||
if db.Statement.Schema.AfterSave {
|
||||
if i, ok := value.(AfterSaveInterface); ok {
|
||||
called = true
|
||||
db.AddError(i.AfterSave(tx))
|
||||
}
|
||||
}
|
||||
|
||||
if db.Statement.Schema.AfterCreate {
|
||||
if i, ok := value.(AfterCreateInterface); ok {
|
||||
called = true
|
||||
db.AddError(i.AfterCreate(tx))
|
||||
}
|
||||
}
|
||||
|
||||
if db.Statement.Schema.AfterSave {
|
||||
if i, ok := value.(AfterSaveInterface); ok {
|
||||
called = true
|
||||
db.AddError(i.AfterSave(tx))
|
||||
}
|
||||
}
|
||||
return called
|
||||
})
|
||||
}
|
||||
|
|
|
@ -29,6 +29,7 @@ func SetupUpdateReflectValue(db *gorm.DB) {
|
|||
}
|
||||
}
|
||||
|
||||
// BeforeUpdate before update hooks
|
||||
func BeforeUpdate(db *gorm.DB) {
|
||||
if db.Error == nil && db.Statement.Schema != nil && !db.Statement.SkipHooks && (db.Statement.Schema.BeforeSave || db.Statement.Schema.BeforeUpdate) {
|
||||
callMethod(db, func(value interface{}, tx *gorm.DB) (called bool) {
|
||||
|
@ -51,6 +52,7 @@ func BeforeUpdate(db *gorm.DB) {
|
|||
}
|
||||
}
|
||||
|
||||
// Update update hook
|
||||
func Update(config *Config) func(db *gorm.DB) {
|
||||
supportReturning := utils.Contains(config.UpdateClauses, "RETURNING")
|
||||
|
||||
|
@ -99,9 +101,17 @@ func Update(config *Config) func(db *gorm.DB) {
|
|||
}
|
||||
}
|
||||
|
||||
// AfterUpdate after update hooks
|
||||
func AfterUpdate(db *gorm.DB) {
|
||||
if db.Error == nil && db.Statement.Schema != nil && !db.Statement.SkipHooks && (db.Statement.Schema.AfterSave || db.Statement.Schema.AfterUpdate) {
|
||||
callMethod(db, func(value interface{}, tx *gorm.DB) (called bool) {
|
||||
if db.Statement.Schema.AfterUpdate {
|
||||
if i, ok := value.(AfterUpdateInterface); ok {
|
||||
called = true
|
||||
db.AddError(i.AfterUpdate(tx))
|
||||
}
|
||||
}
|
||||
|
||||
if db.Statement.Schema.AfterSave {
|
||||
if i, ok := value.(AfterSaveInterface); ok {
|
||||
called = true
|
||||
|
@ -109,12 +119,6 @@ func AfterUpdate(db *gorm.DB) {
|
|||
}
|
||||
}
|
||||
|
||||
if db.Statement.Schema.AfterUpdate {
|
||||
if i, ok := value.(AfterUpdateInterface); ok {
|
||||
called = true
|
||||
db.AddError(i.AfterUpdate(tx))
|
||||
}
|
||||
}
|
||||
return called
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue