This commit is contained in:
谢小军
2020-01-05 21:44:16 +08:00
parent 9a0ac52273
commit 4bebe1c3f1
3 changed files with 32 additions and 0 deletions

View File

@@ -2,6 +2,7 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/xxjwxc/gormt)](https://goreportcard.com/report/github.com/xxjwxc/gormt)
[![codecov](https://codecov.io/gh/xxjwxc/gormt/branch/master/graph/badge.svg)](https://codecov.io/gh/xxjwxc/gormt)
[![GoDoc](https://godoc.org/github.com/xxjwxc/gormt?status.svg)](https://godoc.org/github.com/xxjwxc/gormt)
[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go)
# [gormt](https://github.com/xxjwxc/gormt)

View File

@@ -1,6 +1,7 @@
[![Build Status](https://travis-ci.org/xxjwxc/gormt.svg?branch=master)](https://travis-ci.org/xxjwxc/gormt)
[![Go Report Card](https://goreportcard.com/badge/github.com/xxjwxc/gormt)](https://goreportcard.com/report/github.com/xxjwxc/gormt)
[![GoDoc](https://godoc.org/github.com/xxjwxc/gormt?status.svg)](https://godoc.org/github.com/xxjwxc/gormt)
[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go)
# [gorm-tools](https://github.com/xxjwxc/gormt)

View File

@@ -38,6 +38,36 @@ func (obj *_ExampleMgr) GetTableName() string {
return "example"
}
// Get 获取
func (obj *_ExampleMgr) Get() (result Example, err error) {
err = obj.DB.Table(obj.GetTableName()).Find(&result).Error
if err == nil && obj.isRelated {
var info []User
err = obj.DB.Where("job = ?", result.UserID).Find(&info).Error
if err != nil {
return
}
result.UserList = info
}
return
}
// Gets 获取批量结果
func (obj *_ExampleMgr) Gets() (results []*Example, err error) {
err = obj.DB.Table(obj.GetTableName()).Find(&results).Error
if err == nil && obj.isRelated {
for i := 0; i < len(results); i++ {
var userList []User
err = obj.DB.Where("job = ?", results[i].UserID).Find(&userList).Error
if err != nil {
return
}
results[i].UserList = userList
}
}
return
}
// GetFromID 通过id获取内容
func (obj *_ExampleMgr) GetFromID(id int) (results []*Example, err error) {
err = obj.DB.Table(obj.GetTableName()).Where("id = ?", id).Find(&results).Error