如何选择 Gin-Gorm 中的前 N 个元素

huangapple go评论77阅读模式
英文:

How to select first N elements Gin-Gorm

问题

我有一个显示Categories的函数,我想使用Preload方法来显示与该类别相关的Products,但我只需要5个产品,该如何修改请求?

函数:

func GetAllCategories(c *gin.Context) {
    Categories := []models.Categories{}
    if err := config.DB.Preload("Products").Limit(5).Find(&Categories).Error; err != nil {  
        c.JSON(http.StatusInternalServerError, err.Error())
    } else {
        c.JSON(http.StatusOK, gin.H{"data": &Categories})
    }
}

Categories:

type Categories struct {
    ID         uint       `json:"ID" gorm:"primaryKey"`
    Title      string     `json:"title"`
    ...
    Products   []Products `gorm:"foreignKey:CategoriesRefer" json:"products"`
}

Products:

type Products struct {
    gorm.Model
    CategoriesRefer   int64      `json:"cat_id" gorm:"column:cat_id"`
    Title             string     `json:"title" gorm:"column:title"`
    ...
}
英文:

I have a function that displays Categories, I want to use the Preload method to also display Products related to this category, but I don’t need all the products, but only 5 pieces, how can I fix the request?
Function:

func GetAllCategories(c *gin.Context) {
	Categories := []models.Categories{}
	if err := config.DB.Preload("Products").Find(&Categories).Error; err != nil {  
		c.JSON(http.StatusInternalServerError, err.Error())
	} else {
		c.JSON(http.StatusOK, gin.H{"data": &Categories})
	}
}

Categories:

type Categories struct {
	ID         uint       `json:"ID" gorm:"primaryKey"`
	Title      string     `json:"title"`
	...
	Products   []Products `gorm:"foreignKey:CategoriesRefer" json:"products"`
}

Products:

type Products struct {
	gorm.Model
	CategoriesRefer   int64      `json:"cat_id" gorm:"column:cat_id"`
	Title             string     `json:"title" gorm:"column:title"`
	...
}

答案1

得分: 1

你可以尝试使用自定义预加载(custom preloading)来修改Preload函数加载Products的方式。代码应该类似于以下内容:

func GetAllCategories(c *gin.Context) {
    Categories := []models.Categories{}
    err := config.DB.Preload("Products", func(db *gorm.DB) *gorm.DB {
             return db.Limit(5)
           }).Find(&Categories).Error 
    if err != nil {  
        c.JSON(http.StatusInternalServerError, err.Error())
    } else {
        c.JSON(http.StatusOK, gin.H{"data": &Categories})
    }
}

你可以参考GORM文档了解更多关于自定义预加载的信息。

英文:

You can try custom preloading to modify how the Preload function is going to load Products. The code should look something like this:

func GetAllCategories(c *gin.Context) {
    Categories := []models.Categories{}
    err := config.DB.Preload("Products", func(db *gorm.DB) *gorm.DB {
             return db.Limit(5)
           }).Find(&Categories).Error 
    if err != nil {  
        c.JSON(http.StatusInternalServerError, err.Error())
    } else {
        c.JSON(http.StatusOK, gin.H{"data": &Categories})
    }
}

huangapple
  • 本文由 发表于 2022年7月16日 18:44:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/73003521.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定