本文整理汇总了Golang中github.com/jinzhu/gorm.DB.Find方法的典型用法代码示例。如果您正苦于以下问题:Golang DB.Find方法的具体用法?Golang DB.Find怎么用?Golang DB.Find使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/jinzhu/gorm.DB
的用法示例。
在下文中一共展示了DB.Find方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getUsersPost
func getUsersPost(uid string, allUsers bool, category int64, published string, db *gorm.DB) (posts []models.Post, err error) {
db = db.Preload("Author").Order("created_at desc")
if !allUsers {
db = db.Where("author_id = ?", uid)
}
if category != 0 {
db = db.Joins(
"join post_categories as pc on posts.id = pc.post_id " +
"join categories as c on c.id = pc.category_id")
db = db.Where("c.id = ?", category)
}
published = strings.ToLower(published)
if published != "all" && (published == "true" || published == "false") {
pub := published == "true"
db = db.Where("published = ?", pub)
}
// the Select call is needed because of a bug.
// TODO: remove this call when the bug is fixed
db = db.Select("posts.id, posts.title, posts.content, posts.published, posts.created_at, posts.updated_at, posts.author_id")
if err = db.Find(&posts).Error; err != nil {
return nil, err
}
return posts, nil
}
示例2: ClassifyPost
func ClassifyPost(params martini.Params, res http.ResponseWriter, cls models.Classify, db *gorm.DB) string {
db.Save(&cls)
fi, err := os.Open("./jade/layout-tmpl.jade")
if err != nil {
panic(err)
}
defer fi.Close()
fd, err2 := ioutil.ReadAll(fi)
f, err2 := os.OpenFile("./jade/layout.jade", os.O_RDWR, 0755)
if err2 != nil {
panic(err)
}
css := []models.Classify{}
db.Find(&css)
var buf bytes.Buffer
for _, v := range css {
buf.WriteString(" a(href=\"/" + v.Url + ".html\") " + v.Title + "\n")
}
w := strings.Replace(string(fd), "####", buf.String(), -1)
n, err1 := io.WriteString(f, w) //写入文件(字符串)
if err1 != nil {
panic(err1)
}
fmt.Printf("写入 %d 个字节n", n)
return "1"
}
示例3: PrintTable
func PrintTable(db gorm.DB) {
var users []User
db.Find(&users)
fmt.Printf("%+v\n", users)
}
示例4: GetUsers
/*
GetUsers returns collection of users
*/
func GetUsers(db *gorm.DB) (*[]User, error) {
var users []User
if err := db.Find(&users).Error; err != nil {
return nil, err
}
return &users, nil
}
示例5: importDoc
func importDoc(db gorm.DB, options Options, index, docID string) error {
id, err := strconv.ParseInt(docID, 10, 64)
if err != nil {
return err
}
var doc interface{}
switch index {
case "jobs":
job := application.Job{}
if err := db.Find(&job, id).Error; err != nil {
return err
}
doc = job
case "freelancers":
freelancer := application.Freelancer{}
if err := db.Find(&freelancer, id).Error; err != nil {
return err
}
doc = freelancer
}
if err := importDocument(options, index, docID, doc); err != nil {
return err
}
return nil
}
示例6: GetArtifacts
// GetArtifacts retrieves all the Artifacts created from buildID
func GetArtifacts(db *gorm.DB, sum *summary.Summary) ([]Artifact, int) {
var arts []Artifact
var count int
db.Find(&arts, Artifact{
Summary: *sum,
}).Count(&count)
return arts, count
}
示例7: FacultiesList
func FacultiesList(db *gorm.DB) []*models.Faculties {
var faculties []*models.Faculties
err := db.Find(&faculties).Error
if err != nil {
panic(err)
}
return faculties
}
示例8: DomainsIndex
func DomainsIndex(w http.ResponseWriter, r *http.Request, db gorm.DB) {
domains := []models.Domain{}
db.Find(&domains)
response := Response{"domains", domains}
data, _ := json.Marshal(response)
w.Header().Set("Content-Type", "application/json")
w.Write(data)
}
示例9: getAllUsersHandler
// Returns a json list of all the users
func getAllUsersHandler(db *gorm.DB, ren render.Render) {
users := make([]User, 0, 0)
if err := db.Find(&users).Error; err != nil {
ren.Text(http.StatusInternalServerError, err.Error())
return
}
ren.JSON(http.StatusOK, users)
}
示例10: getAllUsers
func getAllUsers(db *gorm.DB) ([]MaillistUser, error) {
var mus []MaillistUser
err := db.Find(&mus).Error
if err != nil {
return nil, err
}
return mus, nil
}
示例11: GetAllMessages
func GetAllMessages(db *gorm.DB) ([]MaillistMessage, error) {
var mms []MaillistMessage
err := db.Find(&mms).Error
if err != nil {
return nil, err
}
return mms, nil
}
示例12: GetAllVisitorPageRequests
func GetAllVisitorPageRequests(db *gorm.DB) ([]VisitorPageRequest, error) {
var prs []VisitorPageRequest
err := db.Find(&prs).Error
if err != nil {
return nil, err
}
return prs, nil
}
示例13: Resource
// Same of ResourceGorm but for collection, see above
func (_ *CollectionGorm) Resource(h GormHandlers, db *gorm.DB, p gin.Params) (interface{}, error) {
rtype, _ := h.ResourceClass()
res := reflect.New(reflect.SliceOf(rtype)).Interface()
if err := db.Find(res).Error; err != nil {
return nil, err
}
return res, nil
}
示例14: GetAllLike
func GetAllLike(db gorm.DB, router *gin.Engine) {
// GET /like
// GET all like
router.GET("/like", func(c *gin.Context) {
var like []model.Like
db.Find(&like)
c.JSON(http.StatusOK, like)
})
}
示例15: GetAllWallEntry
func GetAllWallEntry(db gorm.DB, router *gin.Engine) {
// GET /wall_entry
// GET all wall_entry
router.GET("/wall_entry", func(c *gin.Context) {
var wall_entry []model.WallEntry
db.Find(&wall_entry)
c.JSON(http.StatusOK, wall_entry)
})
}