xormplus / xorm

xorm是一个简单而强大的Go语言ORM库,通过它可以使数据库操作非常简便。本库是基于原版xorm的定制增强版本,为xorm提供类似ibatis的配置文件及动态SQL支持,支持AcitveRecord操作
BSD 3-Clause "New" or "Revised" License
1.55k stars 222 forks source link

xorm does not support struct inherited? #41

Closed focusonline closed 5 years ago

focusonline commented 5 years ago

for example,

type a struct  {
Id int64 `orm:"pk"
}
type B struct {
a
UserName string `orm:"column:user_name"`
}

func (engine *Engine) mapType(v reflect.Value) (*core.Table, error) {

there are some common fields for many tables like id or createtime and so forth. but common fields in a hidden struct because it can not be used by others. when using B to query, the func mapType will raise error at engine.go:1031 `if , ok := fieldValue.Addr().Interface().(core.Conversion); ok {` will this be fixed to support inheritance for common fields?

focusonline commented 5 years ago

I added some code to skip processing hidden properties and error disappeared, but it can not find common fields as well, always tells me no primiary key...

xormplus commented 5 years ago

Find query multiple records from database, also you can use join and extends

type User struct {
    Id int64
    Name string
    Salt string
    Age int
    Passwd string `xorm:"varchar(200)"`
    Created time.Time `xorm:"created"`
    Updated time.Time `xorm:"updated"`
}

var users []User
err := engine.Where("name = ?", name).And("age > 10").Limit(10, 0).Find(&users)
// SELECT * FROM user WHERE name = ? AND age > 10 limit 10 offset 0

type Detail struct {
    Id int64
    UserId int64 `xorm:"index"`
}

type UserDetail struct {
    User `xorm:"extends"`
    Detail `xorm:"extends"`
}

var users []UserDetail
err := engine.Table("user").Select("user.*, detail.*").
    Join("INNER", "detail", "detail.user_id = user.id").
    Where("user.name = ?", name).Limit(10, 0).
    Find(&users)
// SELECT user.*, detail.* FROM user INNER JOIN detail WHERE user.name = ? limit 10 offset 0
focusonline commented 5 years ago

but Detail and User is real table in your case, I just defined some common fields not a real table as struct so I do not think it can resolve my problem. thank you for your help.