我想处理将要传递的不同url参数(pagelimitcat_idprod_id).

func GeneratePaginationFromRequest(c *gin.Context) models.Pagination {
    limit := 0
    page := 1
    prod_id := 0
    cat_id := 0
    query := c.Request.URL.Query()
    for key, value := range query {
        queryValue := value[len(value)-1]
        switch key {
        case "limit":
            limit, _ = strconv.Atoi(queryValue)
        case "page":
            page, _ = strconv.Atoi(queryValue)
        case "cat_id":
            cat_id, _ = strconv.Atoi(queryValue)
        case "prod_id":
            prod_id, _ = strconv.Atoi(queryValue)
        }
    }
    return models.Pagination{
        Limit:   limit,
        Page:    page,
        Cat_id:  cat_id,
        Prod_id: prod_id,
    }
}
func GetAllIproducts(q *models.Products, pagination *models.Pagination) (*[]models.Products, error) {
    var prod []models.Products
    offset := (pagination.Page - 1) * pagination.Limit
    queryBuider := config.DB.Limit(pagination.Limit).Offset(offset)
    result := queryBuider.Model(&models.Products{}).Where(q.ID, pagination.Prod_id).Where(q.CategoriesRefer, pagination.Cat_id).Find(&prod)
    if result.Error != nil {
        msg := result.Error
        return nil, msg
    }
    return &prod, nil
}

当我使用Debug()查看获得了什么sql查询时,他显示了以下内容:

SELECT * FROM "products" WHERE "products"."id" IN (0,0) AND "products"."id" IN (0,1) AND "products"."deleted_at" IS NULL

他试着拿了两次Product ID.

产品 struct :

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

推荐答案

取而代之的是:

result := queryBuider.Model(&models.Products{}).Where(q.ID, pagination.Prod_id).Where(q.CategoriesRefer, pagination.Cat_id).Find(&prod)

试试这个:

result := config.DB.Debug().Model(&models.Products{}).Where(q).Where("cat_id=?", pagination.Cat_id).Where("prod_id=?", pagination.Prod_id).Limit(pagination.Limit).Offset(offset).Find(&prod)


Go相关问答推荐

获取作为类型参数传递给方法接收方中的类型参数的切片的基础类型

GORM Find方法中缺少字段

将类型定义为泛型类型实例化

在Mac中使用uname获取处理器体系 struct 时,在为AMD64构建Go二进制时出现错误结果

由docker中的nginx提供的样式和图像在页面上不起作用

我可以扫描表中每个项目的最高范围键值吗?

这种合并排序的实现有什么问题?

在 go 中,将接收器 struct 从值更改为指针是否向后兼容?

我无法使用反向代理更改主机标头

Golang 网络应用程序安全性:您是否应该判断输入是否为有效的 utf-8?

Get 请求在 Thunder 客户端/Postman 中返回数据,但在 Golang 代码中给出空白数据

使用 os/exec 和在命令行执行之间的结果莫名其妙地不同

设置指向空接口的指针

如何处理 Go 的 firebase admin sdk 错误?

如何为导入的嵌入式 struct 文字提供值?

如何在测试中使用自定义标志(使用`testify/suite`)

Go GCP 同时模拟两个服务帐户

将 CSVExport 函数传递给处理程序 Gin

在 Go 中将十六进制转换为带符号的 Int

Go 赋值涉及到自定义类型的指针