duanraotun1674 2014-12-10 09:23 采纳率: 100%
浏览 898

如何在beego框架上正确使用RelatedSel()方法

I know this is very basic, but i think the official documentation at [beego website][1]

[1]: http://beego.me/docs/mvc/model/query.md doesn't give clear direction.

I made a RESTful API using beego framework. As it promised, it generates basic CRUD code for my app. The problem is READ method doesn't return all data. By all data I mean data in a table including data from all tables related to it.

this is the output from the generated code (i'm using swagger to call it):

{
  "data": [
    {
      "Id": 1,
      "CustomerId": {
        "Id": 2,
        "Name": "",
        "Phone": "",
        "Email": "",
        "CreatedAt": "0001-01-01T00:00:00Z",
        "UpdatedAt": "0001-01-01T00:00:00Z"
      },
      "Saldo": 2500000,
      "CreatedAt": "2014-12-10T08:10:10+07:00",
      "UpdatedAt": "2014-12-10T08:10:10+07:00"
    }
  ],
  "totals": 1
}

see, it doesn't return the Name, Phone, and Email. so i look into documentation and found this method RelatedSel() but still I have no idea how to use it properly.

here's my code:

func GetAllCustomerSaldo(query map[string]string, fields []string, sortby []string, order []string,
    offset int64, limit int64) (ml []interface{}, err error, totals int64) {
    o := orm.NewOrm()
    qs := o.QueryTable(new(CustomerSaldo))
    qs.RelatedSel("CustomerId__Customers").All(&CustomerSaldo{})
...

after trying many parameter possibilities, i still get this error:

Handler crashed with error unknown model/table name `Customers`

Anyone here have same problem with me? any solution guys?

  • 写回答

1条回答 默认 最新

  • douxian4323 2016-12-02 09:19
    关注

    I had a slightly different problem, but the solution that I have found can help with this problem too. I foun solution here https://github.com/astaxie/beego/issues/1258

    You need to call qs.RelatedSel() without parameters (or with int parameter, that responds about deep of relation selection) and manually call LoadRelated for each record

    func GetAllCustomerSaldo(query map[string]string, fields []string, sortby []string, order []string,
        offset int64, limit int64) (ml []interface{}, err error, totals int64) {
        o := orm.NewOrm()
        qs := o.QueryTable(new(CustomerSaldo))
        qs.RelatedSel().All(&CustomerSaldo{})
        ...
    }
    
    o := orm.NewOrm()
    for _, el := range arr {
       o.LoadRelated(el, "CustomerId")
    }
    

    RelatedSel is like LeftOuterJoin

    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大