duanjia4220 2019-07-06 13:24
浏览 64
已采纳

如何添加关系数据

I start to learn Golang with sample project from https://github.com/dejavuzhou/felix

My first project already runnning well. But I want some custom output. I mean I want to get relation data Has Many when I get order data, but I fail.

Start with my simple case, I have 2 tables (order and detail_order). One order has one or more detail_order.

My handler_order.go

    package handlers

    import (
        "github.com/berthojoris/ginbro/models"
        "github.com/gin-gonic/gin"
    )

    func init() {
        groupApi.GET("order", orderAll)
        groupApi.GET("order/:id", orderOne)
        groupApi.POST("order", orderCreate)
        groupApi.PATCH("order", orderUpdate)
        groupApi.DELETE("order/:id", orderDelete)
    }

    //All
    func orderAll(c *gin.Context) {
        mdl := models.Order{}
        query := &models.PaginationQuery{}
        err := c.ShouldBindQuery(query)
        if handleError(c, err) {
            return
        }
        list, total, err := mdl.All(query)
        if handleError(c, err) {
            return
        }
        jsonPagination(c, list, total, query)
    }

    //One
    func orderOne(c *gin.Context) {
        var mdl models.Order
        id, err := parseParamID(c)
        if handleError(c, err) {
            return
        }
        mdl.Id = id
        data, err := mdl.One()
        if handleError(c, err) {
            return
        }
        jsonData(c, data)
    }

    //Create
    func orderCreate(c *gin.Context) {
        var mdl models.Order
        err := c.ShouldBind(&mdl)
        if handleError(c, err) {
            return
        }
        err = mdl.Create()
        if handleError(c, err) {
            return
        }
        jsonData(c, mdl)
    }

    //Update
    func orderUpdate(c *gin.Context) {
        var mdl models.Order
        err := c.ShouldBind(&mdl)
        if handleError(c, err) {
            return
        }
        err = mdl.Update()
        if handleError(c, err) {
            return
        }
        jsonSuccess(c)
    }

    //Delete
    func orderDelete(c *gin.Context) {
        var mdl models.Order
        id, err := parseParamID(c)
        if handleError(c, err) {
            return
        }
        mdl.Id = id
        err = mdl.Delete()
        if handleError(c, err) {
            return
        }
        jsonSuccess(c)
    }

My model_order.go

package models

import (
    "errors"
    "time"
)

var _ = time.Thursday

//Order
type Order struct {
    Id    uint    `gorm:"column:id" form:"id" json:"id" comment:"" sql:"int(10),PRI"`
    Total float64 `gorm:"column:total" form:"total" json:"total" comment:"" sql:"double"`
    OrderDetail []OrderDetail `gorm:"foreignkey:OrderID" json:"detail_order"`
}

//TableName
func (m *Order) TableName() string {
    return "order"
}

//One
func (m *Order) One() (one *Order, err error) {
    one = &Order{}
    err = crudOne(m, one)
    return
}

//All
func (m *Order) All(q *PaginationQuery) (list *[]Order, total uint, err error) {
    list = &[]Order{}
    total, err = crudAll(m, q, list)
    return
}

//Update
func (m *Order) Update() (err error) {
    where := Order{Id: m.Id}
    m.Id = 0

    return crudUpdate(m, where)
}

//Create
func (m *Order) Create() (err error) {
    m.Id = 0

    return mysqlDB.Create(m).Error
}

//Delete
func (m *Order) Delete() (err error) {
    if m.Id == 0 {
        return errors.New("resource must not be zero value")
    }
    return crudDelete(m)
}

My model_order_detail.go

package models

import (
    "errors"
    "time"
)

var _ = time.Thursday

//OrderDetail
type OrderDetail struct {
    Id        uint    `gorm:"column:id" form:"id" json:"id" comment:"" sql:"int(10),PRI"`
    OrderId   int     `gorm:"column:order_id" form:"order_id" json:"order_id" comment:"" sql:"int(10),MUL"`
    ItemId    int     `gorm:"column:item_id" form:"item_id" json:"item_id" comment:"" sql:"int(10)"`
    ItemName  string  `gorm:"column:item_name" form:"item_name" json:"item_name" comment:"" sql:"varchar(100)"`
    ItemPrice float64 `gorm:"column:item_price" form:"item_price" json:"item_price" comment:"" sql:"double"`
}

//TableName
func (m *OrderDetail) TableName() string {
    return "order_detail"
}

//One
func (m *OrderDetail) One() (one *OrderDetail, err error) {
    one = &OrderDetail{}
    err = crudOne(m, one)
    return
}

//All
func (m *OrderDetail) All(q *PaginationQuery) (list *[]OrderDetail, total uint, err error) {
    list = &[]OrderDetail{}
    total, err = crudAll(m, q, list)
    return
}

//Update
func (m *OrderDetail) Update() (err error) {
    where := OrderDetail{Id: m.Id}
    m.Id = 0

    return crudUpdate(m, where)
}

//Create
func (m *OrderDetail) Create() (err error) {
    m.Id = 0

    return mysqlDB.Create(m).Error
}

//Delete
func (m *OrderDetail) Delete() (err error) {
    if m.Id == 0 {
        return errors.New("resource must not be zero value")
    }
    return crudDelete(m)
}

And my db_helper.go

package models

import (
    "errors"
    "fmt"
    "github.com/jinzhu/gorm"
    "reflect"
    "strconv"
    "strings"
)

//PaginationQuery gin handler query binding struct
type PaginationQuery struct {
    Where  string `form:"where"`
    Fields string `form:"fields"`
    Order  string `form:"order"`
    Offset uint   `form:"offset"`
    Limit  uint   `form:"limit"`
}

//String to string
func (pq *PaginationQuery) String() string {
    return fmt.Sprintf("w=%v_f=%s_o=%s_of=%d_l=%d", pq.Where, pq.Fields, pq.Order, pq.Offset, pq.Limit)
}

func crudAll(m interface{}, q *PaginationQuery, list interface{}) (total uint, err error) {
    var tx *gorm.DB
    total, tx = getResourceCount(m, q)
    if q.Fields != "" {
        columns := strings.Split(q.Fields, ",")
        if len(columns) > 0 {
            tx = tx.Select(q.Fields)
        }
    }
    if q.Order != "" {
        tx = tx.Order(q.Order)
    }
    if q.Offset > 0 {
        tx = tx.Offset(q.Offset)
    }
    if q.Limit <= 0 {
        q.Limit = 15
    }
    err = tx.Limit(q.Limit).Find(list).Error
    return
}

func crudOne(m interface{}, one interface{}) (err error) {
    if mysqlDB.Where(m).First(one).RecordNotFound() {
        return errors.New("resource is not found")
    }
    return nil
}

func crudUpdate(m interface{}, where interface{}) (err error) {
    db := mysqlDB.Model(where).Updates(m)
    if err = db.Error; err != nil {
        return
    }
    if db.RowsAffected != 1 {
        return errors.New("id is invalid and resource is not found")
    }
    return nil
}

func crudDelete(m interface{}) (err error) {
    //WARNING When delete a record, you need to ensure it’s primary field has value, and GORM will use the primary key to delete the record, if primary field’s blank, GORM will delete all records for the model
    //primary key must be not zero value
    db := mysqlDB.Delete(m)
    if err = db.Error; err != nil {
        return
    }
    if db.RowsAffected != 1 {
        return errors.New("resource is not found to destroy")
    }
    return nil
}
func getResourceCount(m interface{}, q *PaginationQuery) (uint, *gorm.DB) {
    var tx = mysqlDB.Model(m)
    conditions := strings.Split(q.Where, ",")
    for _, val := range conditions {
        w := strings.SplitN(val, ":", 2)
        if len(w) == 2 {
            bindKey, bindValue := w[0], w[1]
            if intV, err := strconv.ParseInt(bindValue, 10, 64); err == nil {
                // bind value is int
                field := fmt.Sprintf("`%s` > ?", bindKey)
                tx = tx.Where(field, intV)
            } else if fV, err := strconv.ParseFloat(bindValue, 64); err == nil {
                // bind value is float
                field := fmt.Sprintf("`%s` > ?", bindKey)
                tx = tx.Where(field, fV)
            } else if bindValue != "" {
                // bind value is string
                field := fmt.Sprintf("`%s` LIKE ?", bindKey)
                sV := fmt.Sprintf("%%%s%%", bindValue)
                tx = tx.Where(field, sV)
            }
        }
    }
    modelName := getType(m)
    rKey := redisPrefix + modelName + q.String() + "_count"
    v, err := mem.GetUint(rKey)
    if err != nil {
        var count uint
        tx.Count(&count)
        mem.Set(rKey, count)
        return count, tx
    }
    return v, tx
}

func getType(v interface{}) string {
    t := reflect.TypeOf(v)
    if t.Kind() == reflect.Ptr {
        return "*" + t.Elem().Name()
    }
    return t.Name()
}

In my order model, I add

OrderDetail []OrderDetail `gorm:"foreignkey:OrderID" json:"detail_order"`

For my relation data. And the result is enter image description here

Even though I have the relation data enter image description here

My question, How I add / attach OrderDetail struct inside Order struct So that my table relationship data comes out.

Thanks

  • 写回答

1条回答 默认 最新

  • douguachan2879 2019-07-07 01:43
    关注

    You'd need to add a Preload call into your query or a Related call after you've gotten your object. See the Preload docs here or the Related docs for usage. If you use Preload, which I'd recommend for simplicity, you might have to abandon your crudOne function or adapt it to work with your needs.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败