Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

有个函数看不懂 求解 #44

Open
lyb124553153 opened this issue Apr 21, 2016 · 1 comment
Open

有个函数看不懂 求解 #44

lyb124553153 opened this issue Apr 21, 2016 · 1 comment

Comments

@lyb124553153
Copy link

usermodel里面的
Filter() 里面两个参数 “Role_Role_id” 和roleid 为什么可以这样写

func GetUserByRoleId(roleid int64) (users []orm.Params, count int64) {
    o := orm.NewOrm()
    user := new(User)
    count, _ = o.QueryTable(user).Filter("Role__Role__Id", roleid).Values(&users)
    return users, count
}

"Role_Role_Id" 这个字符串对应的是什么意思

Filter(string, ...interface{}) QuerySeter
    // add NOT condition to querySeter.
    // have the same usage as Filter
@DavadDi
Copy link

DavadDi commented Apr 21, 2016

在reserve(many)或者m2m关系中,因为不是one2one rel(fk)对应关系,所以要指定 fieldname+tablename+tablefiled,
Role__Role__Id 第一个Role表示User对象中的Role []*Role, 第二个Role表示Role对象了,Id就表示Role对象中的Id字段了

例如官方的样例中:
Post 和 Tag 是 ManyToMany 关系
设置 rel(m2m) 以后,ORM会自动创建中间表
type Post struct {
Id int
Title string
User _User orm:"rel(fk)"
Tags []_Tag orm:"rel(m2m)"
}
type Tag struct {
Id int
Name string
Posts []*Post orm:"reverse(many)"
}
通过 tag name 查询哪些 post 使用了这个 tag

var posts []*Post
num, err := dORM.QueryTable("post").Filter("Tags__Tag__Name", "golang").All(&posts)
通过 post title 查询这个 post 有哪些 tag

var tags []*Tag
num, err := dORM.QueryTable("tag").Filter("Posts__Post__Title", "Introduce Beego ORM").All(&tags)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants