fix key search for []string type
This commit is contained in:
14
dbHandler.go
14
dbHandler.go
@@ -182,8 +182,22 @@ func (dH *DBHandler) GetByKey(model any, key string, value any, LikeSearch bool,
|
||||
query = query.Preload(relation)
|
||||
}
|
||||
if LikeSearch {
|
||||
if vals, ok := value.([]string); ok {
|
||||
subQuery := dH.db
|
||||
for i, v := range vals {
|
||||
v = strings.ReplaceAll(v, "*", "%")
|
||||
if i == 0 {
|
||||
subQuery = subQuery.Where(key+" LIKE ?", v)
|
||||
} else {
|
||||
subQuery = subQuery.Or(key+" LIKE ?", v)
|
||||
}
|
||||
}
|
||||
return query.Where(subQuery).Find(model).Error
|
||||
}
|
||||
|
||||
value = strings.ReplaceAll(fmt.Sprint(value), "*", "%")
|
||||
dH.logger.Debug("getByKey", "find like key "+key+" value "+fmt.Sprint(value))
|
||||
fmt.Println(465, "find like key "+key+" value "+fmt.Sprint(value))
|
||||
return query.Where(key+" LIKE ?", value).Find(model).Error
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user