fix some typos in documents (#1680)
* fix typo in models.md * fix typo in crud.md: add lost double quotation * fix markdown code highlight * fix misunderstanding
This commit is contained in:
parent
263ef914f1
commit
d2dfb7d0fb
@ -665,7 +665,7 @@ func (user *User) BeforeSave(scope *gorm.Scope) (err error) {
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
// Add extra SQL option for updating SQL
|
// Add extra SQL option for updating SQL
|
||||||
db.Model(&user).Set("gorm:update_option", "OPTION (OPTIMIZE FOR UNKNOWN)").Update("name, "hello")
|
db.Model(&user).Set("gorm:update_option", "OPTION (OPTIMIZE FOR UNKNOWN)").Update("name", "hello")
|
||||||
//// UPDATE users SET name='hello', updated_at = '2013-11-17 21:34:10' WHERE id=111 OPTION (OPTIMIZE FOR UNKNOWN);
|
//// UPDATE users SET name='hello', updated_at = '2013-11-17 21:34:10' WHERE id=111 OPTION (OPTIMIZE FOR UNKNOWN);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -40,33 +40,41 @@ GORM itself is powered by `Callbacks`, so you could fully customize GORM as you
|
|||||||
|
|
||||||
### Register a new callback
|
### Register a new callback
|
||||||
|
|
||||||
func updateCreated(scope *Scope) {
|
```go
|
||||||
if scope.HasColumn("Created") {
|
func updateCreated(scope *Scope) {
|
||||||
scope.SetColumn("Created", NowFunc())
|
if scope.HasColumn("Created") {
|
||||||
}
|
scope.SetColumn("Created", NowFunc())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
db.Callback().Create().Register("update_created_at", updateCreated)
|
db.Callback().Create().Register("update_created_at", updateCreated)
|
||||||
// register a callback for Create process
|
// register a callback for Create process
|
||||||
|
```
|
||||||
|
|
||||||
### Delete an existing callback
|
### Delete an existing callback
|
||||||
|
|
||||||
db.Callback().Create().Remove("gorm:create")
|
```go
|
||||||
// delete callback `gorm:create` from Create callbacks
|
db.Callback().Create().Remove("gorm:create")
|
||||||
|
// delete callback `gorm:create` from Create callbacks
|
||||||
|
```
|
||||||
|
|
||||||
### Replace an existing callback
|
### Replace an existing callback
|
||||||
|
|
||||||
db.Callback().Create().Replace("gorm:create", newCreateFunction)
|
```go
|
||||||
// replace callback `gorm:create` with new function `newCreateFunction` for Create process
|
db.Callback().Create().Replace("gorm:create", newCreateFunction)
|
||||||
|
// replace callback `gorm:create` with new function `newCreateFunction` for Create process
|
||||||
|
```
|
||||||
|
|
||||||
### Register callback orders
|
### Register callback orders
|
||||||
|
|
||||||
db.Callback().Create().Before("gorm:create").Register("update_created_at", updateCreated)
|
```go
|
||||||
db.Callback().Create().After("gorm:create").Register("update_created_at", updateCreated)
|
db.Callback().Create().Before("gorm:create").Register("update_created_at", updateCreated)
|
||||||
db.Callback().Query().After("gorm:query").Register("my_plugin:after_query", afterQuery)
|
db.Callback().Create().After("gorm:create").Register("update_created_at", updateCreated)
|
||||||
db.Callback().Delete().After("gorm:delete").Register("my_plugin:after_delete", afterDelete)
|
db.Callback().Query().After("gorm:query").Register("my_plugin:after_query", afterQuery)
|
||||||
db.Callback().Update().Before("gorm:update").Register("my_plugin:before_update", beforeUpdate)
|
db.Callback().Delete().After("gorm:delete").Register("my_plugin:after_delete", afterDelete)
|
||||||
db.Callback().Create().Before("gorm:create").After("gorm:before_create").Register("my_plugin:before_create", beforeCreate)
|
db.Callback().Update().Before("gorm:update").Register("my_plugin:before_update", beforeUpdate)
|
||||||
|
db.Callback().Create().Before("gorm:create").After("gorm:before_create").Register("my_plugin:before_create", beforeCreate)
|
||||||
|
```
|
||||||
|
|
||||||
### Pre-Defined Callbacks
|
### Pre-Defined Callbacks
|
||||||
|
|
||||||
|
@ -107,7 +107,7 @@ db.SingularTable(true) // if set this to true, `User`'s default table name will
|
|||||||
|
|
||||||
You can apply any rules on the default table name by defining the `DefaultTableNameHandler`
|
You can apply any rules on the default table name by defining the `DefaultTableNameHandler`
|
||||||
|
|
||||||
```
|
```go
|
||||||
gorm.DefaultTableNameHandler = func (db *gorm.DB, defaultTableName string) string {
|
gorm.DefaultTableNameHandler = func (db *gorm.DB, defaultTableName string) string {
|
||||||
return "prefix_" + defaultTableName;
|
return "prefix_" + defaultTableName;
|
||||||
}
|
}
|
||||||
@ -165,7 +165,7 @@ Save records having `UpdatedAt` field will set it to current time.
|
|||||||
```go
|
```go
|
||||||
// Whenever one or more `user` fields are edited using Save() or Update(), `UpdatedAt` will be set to current time
|
// Whenever one or more `user` fields are edited using Save() or Update(), `UpdatedAt` will be set to current time
|
||||||
db.Save(&user) // will set `UpdatedAt` to current time
|
db.Save(&user) // will set `UpdatedAt` to current time
|
||||||
db.Model(&user).Update("name", "jinzhu") // will set `UpdatedAt` to current time
|
db.Model(&user).Update("name", "jinzhu") // will set `UpdatedAt` to current time
|
||||||
```
|
```
|
||||||
|
|
||||||
### Use `DeletedAt` to store record's deleted time if field exists
|
### Use `DeletedAt` to store record's deleted time if field exists
|
||||||
|
Loading…
x
Reference in New Issue
Block a user