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?