求救Beego大神們,Beego orm 怎麼限制Relation裡返回的Variable?

maplerichie發表於2018-11-17
type Accounts struct {
    Id            int           `orm:"column(account_id);pk"`
    UserName      string        `orm:"column(user_name);null" json:"omitempty"`
    Hash          string        `orm:"column(hash)" json:"omitempty"`
    IsLogin       bool          `orm:"column(is_login)" json:"omitempty"`
    ModifiedAt    time.Time     `orm:"column(modified_at);type(timestamp without time zone)" json:"omitempty"`
    CreatedAt     time.Time     `orm:"column(created_at);type(timestamp without time zone)" json:"omitempty"`
    Status        *DataStatus   `orm:"column(status);rel(fk)"`
    Role          *AccountRoles `orm:"column(role);rel(fk)"`
}

func GetAccountsById(id int) (v *Accounts, err error) {
    o := orm.NewOrm()
    v = &Accounts{Id: id}
    err = o.QueryTable("accounts").Filter("Id", id).RelatedSel().One(v, "account_id", "hash", "is_login", "status", "role")
    if err == nil {
        return v, nil
    }
    return nil, err
}

返回值為

{
    "Id": 1,
    "Hash": "$2a$13$LVfN1o9BTOCIqDBB/bVzg.3NrYRSHNuLaNp5LoJSg124yaq/xMznK",
    "IsLogin": false,
    "Status": {
        "Id": 1,
        "Description": "Account verified with email",
        "ModifiedAt": "2018-11-16T21:11:30.900571Z",
        "CreatedAt": "2018-11-16T21:11:30.900571Z"
    },
    "Role": {
        "Id": 1,
        "Name": "System Administrator",
        "ModifiedAt": "2018-11-16T21:11:29.408474Z",
        "CreatedAt": "2018-11-16T21:11:29.408474Z"
    }
}

理想的狀態為

{
    "Id": 1,
    "Hash": "$2a$13$LVfN1o9BTOCIqDBB/bVzg.3NrYRSHNuLaNp5LoJSg124yaq/xMznK",
    "IsLogin": false,
    "Status": {
        "Id": 1,
        "Description": "Account verified with email"
    },
    "Role": {
        "Id": 1,
        "Name": "System Administrator"
    }
}

請問 Beego ORM 要怎麼 Filter Relation 裡的 Variable? 類似 o.QueryTable("accounts").Filter("Id", id).RelatedSel().One(v, "account_id", "hash", "is_login", "status.data_status_id", "role.role_id")

更多原創文章乾貨分享,請關注公眾號
  • 求救Beego大神們,Beego orm 怎麼限制Relation裡返回的Variable?
  • 加微信實戰群請加微信(註明:實戰群):gocnio

相關文章