社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
案例:
查看陌陌的动态,依次可以按照发布时间,性别,城市进行筛选
如图进行筛选
Method Chaining,Gorm 实现了链式操作接口,所以你可以把代码写成这样:
// 创建一个查询
tx := db.Where("name = ?", "jinzhu")
// 添加更多条件
if someCondition {
tx = tx.Where("age = ?", 20)
} else {
tx = tx.Where("age = ?", 30)
}
if yetAnotherCondition {
tx = tx.Where("active = ?", 1)
}
结合筛选动态的案例,代码如下:
func (xy *Topic) GetTopicListByTimeGenderAddress(createTime, gender int, date, cityName string, adsID int64) (TopicList []Topic) {
//先创建一个查询
tx := Db.Model(&xy).Where("ads_id = ? and status = 1", adsID)
//分类
if adsID > 0 {
tx = tx.Where("ads_id = ?", adsID)
}
//按照创建时间筛选
if createTime > 0 {
tx = tx.Order("create_time desc")
} else {
//按照活动时间筛选
tx = tx.Order("date_timestamp asc")
}
if gender > 0 {
tx = tx.Where("gender = ?", gender)
}
if cityName != "" {
tx = tx.Where("city_name = ?", cityName)
}
tx = tx.Preload("TopicImagesList")
tx.Find(&TopicList).RecordNotFound()
return TopicList
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!