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" | 	"gorm.io/gorm/utils" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | // BeforeCreate before create hooks
 | ||||||
| func BeforeCreate(db *gorm.DB) { | func BeforeCreate(db *gorm.DB) { | ||||||
| 	if db.Error == nil && db.Statement.Schema != nil && !db.Statement.SkipHooks && (db.Statement.Schema.BeforeSave || db.Statement.Schema.BeforeCreate) { | 	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) { | 		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) { | func Create(config *Config) func(db *gorm.DB) { | ||||||
| 	supportReturning := utils.Contains(config.CreateClauses, "RETURNING") | 	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) { | func AfterCreate(db *gorm.DB) { | ||||||
| 	if db.Error == nil && db.Statement.Schema != nil && !db.Statement.SkipHooks && (db.Statement.Schema.AfterSave || db.Statement.Schema.AfterCreate) { | 	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) { | 		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 db.Statement.Schema.AfterCreate { | ||||||
| 				if i, ok := value.(AfterCreateInterface); ok { | 				if i, ok := value.(AfterCreateInterface); ok { | ||||||
| 					called = true | 					called = true | ||||||
| 					db.AddError(i.AfterCreate(tx)) | 					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 | 			return called | ||||||
| 		}) | 		}) | ||||||
| 	} | 	} | ||||||
|  | |||||||
| @ -29,6 +29,7 @@ func SetupUpdateReflectValue(db *gorm.DB) { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | // BeforeUpdate before update hooks
 | ||||||
| func BeforeUpdate(db *gorm.DB) { | func BeforeUpdate(db *gorm.DB) { | ||||||
| 	if db.Error == nil && db.Statement.Schema != nil && !db.Statement.SkipHooks && (db.Statement.Schema.BeforeSave || db.Statement.Schema.BeforeUpdate) { | 	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) { | 		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) { | func Update(config *Config) func(db *gorm.DB) { | ||||||
| 	supportReturning := utils.Contains(config.UpdateClauses, "RETURNING") | 	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) { | func AfterUpdate(db *gorm.DB) { | ||||||
| 	if db.Error == nil && db.Statement.Schema != nil && !db.Statement.SkipHooks && (db.Statement.Schema.AfterSave || db.Statement.Schema.AfterUpdate) { | 	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) { | 		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 db.Statement.Schema.AfterSave { | ||||||
| 				if i, ok := value.(AfterSaveInterface); ok { | 				if i, ok := value.(AfterSaveInterface); ok { | ||||||
| 					called = true | 					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 | 			return called | ||||||
| 		}) | 		}) | ||||||
| 	} | 	} | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user