'Gorm Association Replace condition is not working with where clause and omit() function
Gorm Association Replace condition is not working with where clause and omit() function
func (u *userEnitity) EditUser(emplyeeCode string, user models.User) (models.User, int, string) {
var tx *gorm.DB
var statusCode = 200
var errText string
var count int64
var id int
var roleId []int
count = 0
if tx = u.resource.Select("id").Model(&models.User{}).Where("employee_code = ? ", emplyeeCode).Scan(&id).Count(&count); tx.Error != nil {
return user, statusCode, errText
}
if count == 0 {
return user, 400, "User does not exist"
}
u.resource.Debug().Model(&models.User{}).Where("id = ?", id).Updates(models.User{
FirstName: user.FirstName,
MiddleName: user.MiddleName,
LastName: user.LastName,
MobileNo: user.MobileNo,
Email: user.Email,
})
if tx = u.resource.Table("user_role").Select("role_id").Where("user_id = ?", id).Scan(&roleId); tx.Error != nil {
return user, statusCode, errText
}
u.resource.Debug().Model(&user).Where("id = ?",roleId).Omit("user_role").Association("Roles").Replace(&user.Roles)
return user, statusCode, errText
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
