Beego Raw.QueryRows,模板

时间:2015-08-30 09:21:44

标签: go beego

我尝试使用beego进行电路板分页。

所以我喜欢这样,

在控制器

var articles []*models.Board
board.Data["startCount"] = startCount
board.Data["endCount"] = endCount

/* Template Test Query
num, err := o.QueryTable("board").Filter("Idx", 21).OrderBy("-idx").All(&articles)
*/

num, err := o.Raw("SELECT board.* FROM(SELECT @ROWNUM := @ROWNUM + 1 AS ROWNUM, board.* FROM board, (SELECT @ROWNUM := 0) R) board WHERE board.ROWNUM > ?;", 0).QueryRows(&articles)
if num == 0 || err == orm.ErrNoRows {
    board.Data["user_error"] = err
}
if err != orm.ErrNoRows && num > 0 {
    board.Data["nums"] = num
    board.Data["articles"] = articles
}

在veiw文件中

<tbody>
    {{ if not .articles }}
    <tr>
      <td colspan="5">No articles.</td>
    </tr>
    {{ else }}
    {{ $uri := .article_uri}}
    {{range $articles := .articles}}
    <tr>
      <td>{{$articles.Idx}}</td>
      <td><a href="{{urlfor "BoardController.Content" ":id" $articles.Idx}}">{{$articles.Title}}</a></td>
      <td>{{$articles.Nick}}</td>
      <td>{{date $articles.Date "y.m.d H:i"}}</td>
      <td>0</td>
    </tr>
    {{ end }}
    {{ end }}
</tbody>

当我使用模板测试查询时,我看到成功打印。

但我只能看到空表,因为要改为o.Raw(...)。。QueryRows(&amp; articles)

Plz有人帮助我..

Current status

模型

type Board struct{
Idx int64       `orm:"pk";form:"-"`
Id string       `form:"id"`
Title string    `form:"title"`
Body string     `form:"content"`
Nick string     `form:"nick"`
Date time.Time  `orm:"auto_now_add;type(datetime)"`
}

table

1 个答案:

答案 0 :(得分:0)

在控制器

```
o := orm.NewOrm()
var maps []orm.Params
num, err := o.Raw("SELECT board.* FROM(SELECT @ROWNUM := @ROWNUM + 1 AS ROWNUM, board.* FROM board, (SELECT @ROWNUM := 0) R) board WHERE board.ROWNUM > ?;", 0).Values(&maps)
fmt.Println(maps)
if num == 0 || err == orm.ErrNoRows {
    board.Data["user_error"] = err
}
if err != orm.ErrNoRows && num > 0 {
    board.Data["nums"] = num
    board.Data["articles"] = articles
}
```

ValuesFlat

```
var list orm.ParamsList
num, err = o.Raw(""SELECT board.* FROM(SELECT @ROWNUM := @ROWNUM + 1 AS ROWNUM, board.* FROM board, (SELECT @ROWNUM := 0) R) board WHERE board.ROWNUM > ?;", 0).ValuesFlat(&list)
if err == nil && num > 0 {
    fmt.Println(list) // []{"1","2","3",...}
}
```
  

请参阅演示网址https://beego.me/docs/mvc/model/rawsql.md#queryrows

但我喜欢使用JSON

```
  o := orm.NewOrm()   
        var maps []orm.Params
        o.Raw("SELECT board.* FROM(SELECT @ROWNUM := @ROWNUM + 1 AS ROWNUM, board.* FROM board, (SELECT @ROWNUM := 0) R) board WHERE board.ROWNUM > ?;", 0).Values(&maps)
        list := make([]map[string]interface{}, len(maps))   
        for k, v := range maps {
            row := make(map[string]interface{})
            row["Idx"]=v["workid"]
            row["Id"] = v["droneid"]
            row["Title"]=v["starttime"]
            row["Body"]=v["worktime"]
            row["Nick"]=v["modes"]
            row["Date"]=v["tele"]
            paths := make([]interface{}, 0)
            patharray := strings.Split(v["path"].(string),"],[") 
            for _, vv := range patharray {
                path := make([]string, 2)
                vv1 := strings.Replace(vv,"[", "", -1)
                vv2 := strings.Replace(vv1,"]","",-1)
                //fmt.Println(vv2)
                vvarr := strings.Split(vv2,",")
            //  fmt.Println("++++++++++++++")
                // fmt.Println(vvarr)
                path[0] = vvarr[0]
                path[1] = vvarr[1]
                paths = append(paths, path)
            }
            row["path"] = paths
            list[k] = row
        }
        this.Data["json"] = list
         this.ServeJSON(); 
```