improved docs
This commit is contained in:
parent
e4f05916cb
commit
929df107f1
|
@ -4,7 +4,7 @@
|
|||
新特性:
|
||||
* 移动xorm cmd [github.com/go-xorm/cmd](github.com/go-xorm/cmd)
|
||||
* 在重构一般DB操作核心库 [github.com/go-xorm/core](https://github.com/go-xorm/core)
|
||||
* 移动测试github.com/复XORM/测试 [github.com/go-xorm/tests](github.com/go-xorm/tests)
|
||||
* 移动测试github.com/XORM/tests [github.com/go-xorm/tests](github.com/go-xorm/tests)
|
||||
|
||||
改进:
|
||||
* Prepared statement 缓存
|
||||
|
|
|
@ -693,7 +693,7 @@ Please visit [https://github.com/go-xorm/xorm/tree/master/examples](https://gith
|
|||
<a name="160"></a>
|
||||
## 15.FAQ
|
||||
|
||||
1.How the xorm tag use both with json?
|
||||
* How the xorm tag use both with json?
|
||||
|
||||
Use space.
|
||||
|
||||
|
@ -702,3 +702,38 @@ type User struct {
|
|||
Name string `json:"name" xorm:"name"`
|
||||
}
|
||||
```
|
||||
|
||||
* Does xorm support composite primary key?
|
||||
|
||||
Yes. You can use pk tag. All fields have tag will as one primary key by fields order on struct. When use, you can use xorm.PK{1, 2}. For example: `Id(xorm.PK{1, 2})`.
|
||||
|
||||
* How to use join?
|
||||
|
||||
We can use Join() and extends tag to do join operation. For example:
|
||||
|
||||
type Userinfo struct {
|
||||
Id int64
|
||||
Name string
|
||||
DetailId int64
|
||||
}
|
||||
|
||||
type Userdetail struct {
|
||||
Id int64
|
||||
Gender int
|
||||
}
|
||||
|
||||
type User struct {
|
||||
Userinfo `xorm:"extends"`
|
||||
Userdetail `xorm:"extends"`
|
||||
}
|
||||
|
||||
var users = make([]User, 0)
|
||||
err := engine.Table(&Userinfo{}).Join("LEFT", "userdetail", "userinfo.detail_id = userdetail.id").Find(&users)
|
||||
|
||||
//assert(User.Userinfo.Id != 0 && User.Userdetail.Id != 0)
|
||||
|
||||
Of course, If join statment is very long, you could directly use Sql():
|
||||
|
||||
err := engine.Sql("select * from userinfo, userdetail where userinfo.detail_id = userdetail.id").Find(&users)
|
||||
|
||||
//assert(User.Userinfo.Id != 0 && User.Userdetail.Id != 0)
|
||||
|
|
|
@ -830,6 +830,32 @@ money float64 `xorm:"Numeric"`
|
|||
|
||||
答:支持。在定义时,如果有多个字段标记了pk,则这些字段自动成为复合主键,顺序为在struct中出现的顺序。在使用Id方法时,可以用`Id(xorm.PK{1, 2})`的方式来用。
|
||||
|
||||
* xorm如何使用Join?
|
||||
|
||||
答:一般我们配合Join()和extends标记来进行,比如我们要对两个表进行Join操作,我们可以这样:
|
||||
|
||||
type Userinfo struct {
|
||||
Id int64
|
||||
Name string
|
||||
DetailId int64
|
||||
}
|
||||
|
||||
type Userdetail struct {
|
||||
Id int64
|
||||
Gender int
|
||||
}
|
||||
|
||||
type User struct {
|
||||
Userinfo `xorm:"extends"`
|
||||
Userdetail `xorm:"extends"`
|
||||
}
|
||||
|
||||
var users = make([]User, 0)
|
||||
err := engine.Table(&Userinfo{}).Join("LEFT", "userdetail", "userinfo.detail_id = userdetail.id").Find(&users)
|
||||
|
||||
当然,如果Join语句比较复杂,我们也可以直接用Sql函数
|
||||
|
||||
err := engine.Sql("select * from userinfo, userdetail where userinfo.detail_id = userdetail.id").Find(&users)
|
||||
|
||||
<a name="170" id="170"></a>
|
||||
## 17.讨论
|
||||
|
|
Loading…
Reference in New Issue