我从https://github.com/dejavuzhou/felix的示例项目开始学习Golang。
我的第一个项目已经运行良好。但是我想要一些自定义输出。我的意思是我想在获取订单数据时获取关联数据有很多,但失败了。
从我的简单案例开始,我有2个表(订单和 detail_order )。一个订单具有一个或多个detail_order。
我的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)
}
我的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)
}
我的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)
}
还有我的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()
}
在我的订单模型中,添加
OrderDetail []OrderDetail `gorm:"foreignkey:OrderID" json:"detail_order"`
我的问题是,如何在OrderDetail struct
内添加/附加Order struct
以便表关系数据出来。
谢谢
答案 0 :(得分:2)
您需要在查询中添加Preload
调用,或者在获得对象后添加Related
调用。有关用法,请参见the Preload
docs here或the Related
docs。如果您使用Preload
(为简便起见,建议您使用此功能),则可能不得不放弃crudOne
函数或使其适应您的需求。