接下来关于gf ORM提供的模型关联实现,从GF v1.13.6版本开始提供,目前属于实验性特性。

那么我们就使用一个例子来介绍gf ORM提供的模型关联吧。

为简化示例,我们这里设计得表都尽可能简单,每张表仅包含3-4个字段,方便阐述关联关系即可。

  1. CREATE TABLE `user_detail` (
  2. uid int(10) unsigned NOT NULL AUTO_INCREMENT,
  3. address varchar(45) NOT NULL,
  4. PRIMARY KEY (uid)
  5. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  1. CREATE TABLE `user_scores` (
  2. id int(10) unsigned NOT NULL AUTO_INCREMENT,
  3. uid int(10) unsigned NOT NULL,
  4. score int(10) unsigned NOT NULL,
  5. course varchar(45) NOT NULL,
  6. PRIMARY KEY (id)
  7. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

根据表定义,我们可以得知:

  1. 用户表与用户详情是1:1关系。
  2. 用户表与用户学分是1:N关系。
  3. 这里并没有演示N:N的关系,因为相比较于1:N的查询只是多了一次关联、或者一次查询,最终处理方式和1:N类似。

那么Golang的模型可定义如下:

写入数据时涉及到简单的数据库事务即可。

  1. err := db.Transaction(func(tx *gdb.TX) error {
  2. r, err := tx.Table("user").Save(EntityUser{
  3. Name: "john",
  4. })
  5. if err != nil {
  6. return err
  7. }
  8. if err != nil {
  9. return err
  10. }
  11. _, err = tx.Table("user_detail").Save(EntityUserDetail{
  12. Uid: int(uid),
  13. })
  14. if err != nil {
  15. return err
  16. }
  17. _, err = tx.Table("user_scores").Save(g.Slice{
  18. EntityUserScores{Uid: int(uid), Score: 100, Course: "math"},
  19. EntityUserScores{Uid: int(uid), Score: 99, Course: "physics"},
  20. })
  21. return err
  22. })
  1. // 定义用户列表
  2. var user Entity
  3. // 查询用户基础数据
  4. // SELECT * FROM `user` WHERE `name`='john'
  5. err := db.Table("user").Scan(&user.User, "name", "john")
  6. if err != nil {
  7. return err
  8. }
  9. // 查询用户详情数据
  10. // SELECT * FROM `user_detail` WHERE `uid`=1
  11. err := db.Table("user_detail").Scan(&user.UserDetail, "uid", user.User.Uid)
  12. // 查询用户学分数据
  13. // SELECT * FROM `user_scores` WHERE `uid`=1
  14. err := db.Table("user_scores").Scan(&user.UserScores, "uid", user.User.Uid)

查询单条模型数据比较简单,直接使用Scan方法即可,该方法会自动识别绑定查询结果到单个对象属性还是数组对象属性中。该方法在之前的章节中已经有介绍,因此这里不再赘述。

是不是比较简单。这其中涉及到两个比较重要的方法:

1. ScanList

方法定义:

  1. // ScanList converts <r> to struct slice which contains other complex struct attributes.
  2. // Note that the parameter <listPointer> should be type of *[]struct/*[]*struct.
  3. //
  4. // type Entity struct {
  5. // User *EntityUser
  6. // UserDetail *EntityUserDetail
  7. // UserScores []*EntityUserScores
  8. // }
  9. // var users []*Entity
  10. // or
  11. // var users []Entity
  12. //
  13. // ScanList(&users, "User")
  14. // ScanList(&users, "UserDetail", "User", "uid:Uid")
  15. // ScanList(&users, "UserScores", "User", "uid:Uid")
  16. // The parameters "User"/"UserDetail"/"UserScores" in the example codes specify the target attribute struct
  17. // that current result will be bound to.
  18. // The "uid" in the example codes is the table field name of the result, and the "Uid" is the relational
  19. // struct attribute name. It automatically calculates the HasOne/HasMany relationship with given <relation>
  20. // parameter.
  21. func (m *Model) ScanList(listPointer interface{}, attributeName string, relation ...string) (err error)

该方法用于将查询到的数组数据绑定到指定的列表上,例如:

  • ScanList(&users, "User")
  • ScanList(&users, , "UserDetail", "User", "uid:Uid")

表示将查询到用户详情数组数据绑定到users列表中每一项的UserDetail属性上,并且和另一个User对象属性通过uid:Uid字段:属性关联,内部将会根据这一关联关系自动进行数据绑定。其中uid:Uid前面的uid表示查询结果字段中的uid字段,后面的Uid表示目标关联对象中的Uid属性。

  • ScanList(&users, , "UserScores", "User", "uid:Uid")

表示将查询到用户详情数组数据绑定到users列表中每一项的UserScores属性上,并且和另一个User对象属性通过uid:Uid字段:属性关联,内部将会根据这一关联关系自动进行数据绑定。由于UserScores是一个数组类型[]*EntityUserScores,因此该方法内部可以自动识别到UserUserScores其实是1:N的关系,自动完成数据绑定。

需要提醒的是,如果关联数据中对应的关联属性数据不存在,那么该属性不会被初始化并将保持nil

2. ListItemValues

方法定义:

  1. // ListItemValues retrieves and returns the elements of all item struct/map with key <key>.
  2. // Note that the parameter <list> should be type of slice which contains elements of map or struct,
  3. // or else it returns an empty slice.
  4. //
  5. // The parameter <list> supports types like:
  6. // []map[string]interface{}
  7. // []map[string]sub-map
  8. // []struct
  9. // []struct:sub-struct
  10. func ListItemValues(list interface{}, key interface{}, subKey ...interface{}) (values []interface{})

gdb.ListItemValues(users, "User", "Uid")用于获取users数组中,每一个User属性项中的Uid属性,构造成[]interface{}数组返回。这里以便根据构造成SELECT...IN...查询。