dongmu1914 2019-02-26 11:37
浏览 50

如何在不过滤数据的地方进行预加载

how to work with gorm with preload and filtering i have stuck for some time, maybe can help me

type JualmstModel struct {
    Trnjualmstoid int            `json:"jualid"`
    Trnno         string         `json:"jualno"`
    JualdtlModel  []JualdtlModel `gorm:"foreignkey:Trnjualmstoid;association_foreignkey:Trnjualmstoid"`
}

type JualdtlModel struct {
    Trnjualdtloid          int       `json:"jualdtlid"`
    Trnjualmstoid          int       `json:"jualid"`
    Item                   ItemModel `gorm:"foreignkey:itemoid;association_foreignkey:itemoid"`
    Itemoid                int       `json:"Itemoid"`
}

type ItemModel struct {
    Itemoid  string `json:"itemid"`
    Itemdesc string `json:"itemdesc"`
}

here is the model

DB.Find(b).
        Preload("JualdtlModel.Item", "itemdesc like ?", "%"+menu+"%").
        Find(b)

here the result

 "jualid": 1,
      "jualno": "INV/201902/0001",
      "JualdtlModel": [
        {
          "jualdtlid": 1,
          "jualid": 1,
          "Item": {
            "itemid": "22",
            "itemdesc": "+ KEJU"
          },
          "Itemoid": 22
        },
        {
          "jualdtlid": 2,
          "jualid": 1,
          "Item": {
            "itemid": "",
            "itemdesc": ""
          },
          "Itemoid": 87
        },

i want to filter the item with some menu item, but when i add filter item it not filter all of my query

here debug result

SELECT * FROM [trnJualMst] 

SELECT * FROM [trnJualDtl]  WHERE ([trnjualmstoid] IN (1,2,3)) 

SELECT * FROM [mstItem]  WHERE ([itemoid] IN (87,85,18,12,75,22,19,37,44,36,28,29,92,54,34)) AND (itemdesc like '%keju%')

What can I do so I can only load item that in filter?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 求数学坐标画圆以及直线的算法
    • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
    • ¥15 名为“Product”的列已属于此 DataTable
    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 自己瞎改改,结果现在又运行不了了
    • ¥15 链式存储应该如何解决
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站