2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-06-06 14:38:05 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gdb_test
|
|
|
|
|
|
|
|
import (
|
2021-05-19 21:11:51 +08:00
|
|
|
"context"
|
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
2020-06-06 14:38:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Example_transaction() {
|
2022-05-10 15:38:08 +08:00
|
|
|
g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
|
2020-06-06 14:38:05 +08:00
|
|
|
// user
|
|
|
|
result, err := tx.Insert("user", g.Map{
|
|
|
|
"passport": "john",
|
|
|
|
"password": "12345678",
|
|
|
|
"nickname": "JohnGuo",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// user_detail
|
|
|
|
id, err := result.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = tx.Insert("user_detail", g.Map{
|
|
|
|
"uid": id,
|
|
|
|
"site": "https://johng.cn",
|
|
|
|
"true_name": "GuoQiang",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|