1
0
mirror of https://github.com/beego/bee.git synced 2024-11-23 01:30:55 +00:00

自动生成的GetAllxxxx方法修改结构,添加total信息

This commit is contained in:
吴文豪 2016-11-12 18:56:34 +08:00
parent 50fafd72e8
commit c9443f4ca3
2 changed files with 27 additions and 6 deletions

View File

@ -1055,7 +1055,7 @@ func Get{{modelName}}ById(id int) (v *{{modelName}}, err error) {
// GetAll{{modelName}} retrieves all {{modelName}} matches certain condition. Returns empty list if // GetAll{{modelName}} retrieves all {{modelName}} matches certain condition. Returns empty list if
// no records exist // no records exist
func GetAll{{modelName}}(query map[string]string, fields []string, sortby []string, order []string, func GetAll{{modelName}}(query map[string]string, fields []string, sortby []string, order []string,
offset int64, limit int64) (ml []interface{}, err error) { offset int64, limit int64) (ml map[string]interface{}, err error) {
o := orm.NewOrm() o := orm.NewOrm()
qs := o.QueryTable(new({{modelName}})) qs := o.QueryTable(new({{modelName}}))
// query k=v // query k=v
@ -1105,10 +1105,17 @@ func GetAll{{modelName}}(query map[string]string, fields []string, sortby []stri
var l []{{modelName}} var l []{{modelName}}
qs = qs.OrderBy(sortFields...) qs = qs.OrderBy(sortFields...)
total, err := qs.Count()
if err != nil {
panic(err)
}
if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil { if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil {
var result []interface{}
if len(fields) == 0 { if len(fields) == 0 {
for _, v := range l { for _, v := range l {
ml = append(ml, v) result = append(result, v)
} }
} else { } else {
// trim unused fields // trim unused fields
@ -1118,9 +1125,13 @@ func GetAll{{modelName}}(query map[string]string, fields []string, sortby []stri
for _, fname := range fields { for _, fname := range fields {
m[fname] = val.FieldByName(fname).Interface() m[fname] = val.FieldByName(fname).Interface()
} }
ml = append(ml, m) result = append(result, m)
} }
} }
var ml map[string]interface{} = make(map[string]interface{})
ml["result"] = result
ml["total"] = total
return ml, nil return ml, nil
} }
return nil, err return nil, err

View File

@ -175,7 +175,7 @@ func Get{{modelName}}ById(id int64) (v *{{modelName}}, err error) {
// GetAll{{modelName}} retrieves all {{modelName}} matches certain condition. Returns empty list if // GetAll{{modelName}} retrieves all {{modelName}} matches certain condition. Returns empty list if
// no records exist // no records exist
func GetAll{{modelName}}(query map[string]string, fields []string, sortby []string, order []string, func GetAll{{modelName}}(query map[string]string, fields []string, sortby []string, order []string,
offset int64, limit int64) (ml []interface{}, err error) { offset int64, limit int64) (ml map[string]interface{}, err error) {
o := orm.NewOrm() o := orm.NewOrm()
qs := o.QueryTable(new({{modelName}})) qs := o.QueryTable(new({{modelName}}))
// query k=v // query k=v
@ -225,10 +225,17 @@ func GetAll{{modelName}}(query map[string]string, fields []string, sortby []stri
var l []{{modelName}} var l []{{modelName}}
qs = qs.OrderBy(sortFields...) qs = qs.OrderBy(sortFields...)
total, err := qs.Count()
if err != nil {
panic(err)
}
if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil { if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil {
var result []interface{}
if len(fields) == 0 { if len(fields) == 0 {
for _, v := range l { for _, v := range l {
ml = append(ml, v) result = append(result, v)
} }
} else { } else {
// trim unused fields // trim unused fields
@ -238,9 +245,12 @@ func GetAll{{modelName}}(query map[string]string, fields []string, sortby []stri
for _, fname := range fields { for _, fname := range fields {
m[fname] = val.FieldByName(fname).Interface() m[fname] = val.FieldByName(fname).Interface()
} }
ml = append(ml, m) result = append(result, m)
} }
} }
var ml map[string]interface{} = make(map[string]interface{})
ml["result"] = result
ml["total"] = total
return ml, nil return ml, nil
} }
return nil, err return nil, err