1

在以下模型中:

package models

import "github.com/astaxie/beego/orm"

type Movie struct {
    Id      int    `orm:"pk; auto; column(id)"; form: "-"`
    Title   string `orm:"unique; column(title)"; form: "title, text, title:`
    Plot    string `orm:"column(plot)"; form: "plot, text, plot:"`
    ImdbID  string `orm:"column(imdb_id)"; form: "imdb_id, text, imdb_id:"`
    Actors  string `orm:"column(actors)"; form: "actors, text, actors:"`
    Runtime string `orm:"column(runtime)"; form: "runtime, text, runtime:"`
    Year    string `orm:"column(year)"; form: "year, text, year:"`
    Genre   *Genre `orm:"rel(fk); on_delete(do_nothing)"`
    Cover   string `orm:"column(cover)"; form: "cover, text, cover:"`
    Status  int    `orm:"column(status)"; form: "status, int, status:"`
}

func (a *Movie) TableName() string {
    return "app_movie"
}

func init() {
    orm.RegisterModel(new(Movie))
}

我想参考:流派模型,它看起来像:

package models

import "github.com/astaxie/beego/orm"

type Genre struct {
    Id     int    `orm:"pk; auto; column(id)"; form: "-"`
    Title  string `orm:"unique; column(title)"; form: "title, text, title:`
    Status int    `orm:"column(status)"; form: "status, int, status:"`
}

func (a *Genre) TableName() string {
    return "app_genre"
}

func init() {
    orm.RegisterModel(new(Genre))
}

有关信息,它会转到带有数据的 sqlite 数据库(首先是数据库)。

在控制器中,我遍历电影列表并打印出:movie.Genre.Title 但它是空的。如果我看电影。类型我得到:{3 0}

如何获取标题和 ID?或者。我究竟做错了什么?

谢谢你的帮助!

4

1 回答 1

2
num, err := qs
    .Limit(4)
    .Filter("status", true)
    .RelatedSel()
    .Exclude("useritem__userid", 1)
    .OrderBy("-id")
    .All(&movies)

一切正常。:).RelatedSel()

于 2015-11-21T20:50:46.460 回答