Update callback document
This commit is contained in:
parent
6bc050c2e0
commit
0a92f14d87
@ -2,51 +2,66 @@
|
|||||||
|
|
||||||
<!-- toc -->
|
<!-- toc -->
|
||||||
|
|
||||||
Callbacks are methods defined on the pointer of struct.
|
You could define callback methods to pointer of model struct, it will be called when creating, updating, querying, deleting, if any callback returns an error, gorm will stop future operations and rollback all changes.
|
||||||
If any callback returns an error, gorm will stop future operations and rollback all changes.
|
|
||||||
|
|
||||||
Here is the list of all available callbacks:
|
|
||||||
(listed in the same order in which they will get called during the respective operations)
|
|
||||||
|
|
||||||
### Creating An Object
|
### Creating An Object
|
||||||
|
|
||||||
|
Available Callbacks for creating
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
// begin transaction
|
||||||
BeforeSave
|
BeforeSave
|
||||||
BeforeCreate
|
BeforeCreate
|
||||||
// save before associations
|
// save before associations
|
||||||
|
// update timestamp `CreatedAt`, `UpdatedAt`
|
||||||
// save self
|
// save self
|
||||||
|
// reload fields that have default value and its value is blank
|
||||||
// save after associations
|
// save after associations
|
||||||
AfterCreate
|
AfterCreate
|
||||||
AfterSave
|
AfterSave
|
||||||
|
// commit or rollback transaction
|
||||||
```
|
```
|
||||||
|
|
||||||
### Updating An Object
|
### Updating An Object
|
||||||
|
|
||||||
|
Available Callbacks for updating
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
// begin transaction
|
||||||
BeforeSave
|
BeforeSave
|
||||||
BeforeUpdate
|
BeforeUpdate
|
||||||
// save before associations
|
// save before associations
|
||||||
|
// update timestamp `UpdatedAt`
|
||||||
// save self
|
// save self
|
||||||
// save after associations
|
// save after associations
|
||||||
AfterUpdate
|
AfterUpdate
|
||||||
AfterSave
|
AfterSave
|
||||||
|
// commit or rollback transaction
|
||||||
```
|
```
|
||||||
|
|
||||||
### Destroying An Object
|
### Deleting An Object
|
||||||
|
|
||||||
|
Available Callbacks for deleting
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
// begin transaction
|
||||||
BeforeDelete
|
BeforeDelete
|
||||||
// delete self
|
// delete self
|
||||||
AfterDelete
|
AfterDelete
|
||||||
|
// commit or rollback transaction
|
||||||
```
|
```
|
||||||
|
|
||||||
### After Find
|
### Querying An Object
|
||||||
|
|
||||||
|
Available Callbacks for querying
|
||||||
|
|
||||||
```go
|
```go
|
||||||
// load data from database
|
// load data from database
|
||||||
|
// Preloading (edger loading)
|
||||||
AfterFind
|
AfterFind
|
||||||
```
|
```
|
||||||
|
|
||||||
### Example
|
### Callback Examples
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (u *User) BeforeUpdate() (err error) {
|
func (u *User) BeforeUpdate() (err error) {
|
||||||
@ -65,10 +80,8 @@ func (u *User) AfterCreate() (err error) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Save/delete operations in gorm are running in a transaction.
|
Save/Delete operations in gorm are running in transactions, so changes made in that transaction are not visible unless it is commited.
|
||||||
Changes made in that transaction are not visible unless it is commited.
|
If you want to use those changes in your callbacks, you need to run your SQL in the same transaction. So you need to pass current transaction to callbacks like this:
|
||||||
So if you want to use those changes in your callbacks, you need to run your SQL in the same transaction.
|
|
||||||
For this Gorm supports passing transactions to callbacks like this:
|
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (u *User) AfterCreate(tx *gorm.DB) (err error) {
|
func (u *User) AfterCreate(tx *gorm.DB) (err error) {
|
||||||
@ -76,3 +89,10 @@ func (u *User) AfterCreate(tx *gorm.DB) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (u *User) AfterCreate(scope *gorm.Scope) (err error) {
|
||||||
|
scope.DB().Model(u).Update("role", "admin")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
```
|
||||||
|
Loading…
x
Reference in New Issue
Block a user