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

huangapple go评论92阅读模式
英文:

How to use RelatedSel() method on beego framework properly

问题

我知道这很基础,但我认为[beego网站][1]上的官方文档并没有给出清晰的指导。

我使用beego框架创建了一个RESTful API。正如它所承诺的那样,它为我的应用程序生成了基本的CRUD代码。问题是READ方法并没有返回所有的数据。我所说的所有数据是指一个表中包括与之相关的所有表的数据。

这是生成的代码的输出(我使用swagger来调用它):

{
"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
}

看,它没有返回Name、Phone和Email。所以我查看了文档,找到了RelatedSel()方法,但我仍然不知道如何正确使用它。

这是我的代码:

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{})
...

尝试了很多参数组合后,我仍然得到这个错误:

Handler crashed with error unknown model/table name Customers

这里有人和我遇到了同样的问题吗?有解决办法吗?

英文:

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

得分: 1

我遇到了一个稍微不同的问题,但我找到的解决方案也可以帮助解决这个问题。我在这里找到了解决方案:https://github.com/astaxie/beego/issues/1258

你需要调用qs.RelatedSel()而不带参数(或带有整数参数,用于指定关联选择的深度),然后手动为每个记录调用LoadRelated方法。

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类似于LeftOuterJoin。

英文:

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

答案2

得分: 0

在RelatedSel之后不需要使用LoadRelated,因为RelatedSel会在一次数据库请求中自动获取数据(当然,前提是正确使用)。LoadRelated也会获取相关数据,但需要额外的请求。如果你在使用RelatedSel之后再使用LoadRelated,那么你将会做重复的工作。

orm.Debug = true

o := orm.NewOrm()
qs := o.QueryTable(&models.MainModel{})

var req []*models.RelatedModel

num, err := qs.RelatedSel().All(&req)

beego.Debug(num)

if err != nil {
  beego.Error(err)
}
英文:

It's not necessery to LoadRelated after RelatedSel - because RelatedSel will automatically get data in one request to db (of course if it's correctly used). LoadRelated - do the same but in additional Requests. When You use RelatedSel, and after That LoadRelated - You will do things twice.

  orm.Debug = true
    
  o := orm.NewOrm()
  qs := o.QueryTable(&models.MainModel{})
    
  var req []*models.RelatedModel
    
  num, err := qs.RelatedSel().All(&req)
    
  beego.Debug(num)
  
  if err != nil {
    beego.Error(err)
  }

huangapple
  • 本文由 发表于 2014年12月10日 17:23:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/27397306.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定