如何使用预加载和过滤功能对gorm进行处理我已经停留了一段时间,也许可以帮到我
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"`
}
这是模型
DB.Find(b).
Preload("JualdtlModel.Item", "itemdesc like ?", "%"+menu+"%").
Find(b)
这里是结果
"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
},
我想使用某些菜单项来过滤该项目,但是当我添加过滤器项时,它并不能过滤所有查询
此处调试结果
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%')
该怎么办,这样我只能将其加载到过滤器中?