当前位置: 首页>>代码示例>>Golang>>正文


Golang DB.Find方法代码示例

本文整理汇总了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
}
开发者ID:bertzzie,项目名称:obrolansubuh-backend,代码行数:30,代码来源:post.go

示例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"
}
开发者ID:bishen,项目名称:blog,代码行数:28,代码来源:blog.go

示例3: PrintTable

func PrintTable(db gorm.DB) {
	var users []User

	db.Find(&users)

	fmt.Printf("%+v\n", users)
}
开发者ID:GoSteelProgrammers,项目名称:talk-review-orms,代码行数:7,代码来源:crud.go

示例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
}
开发者ID:IIC2173-2015-2-Grupo2,项目名称:news-api,代码行数:10,代码来源:user.go

示例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
}
开发者ID:fairlance,项目名称:backend,代码行数:26,代码来源:requests.go

示例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
}
开发者ID:ZombieHippie,项目名称:test-gin,代码行数:9,代码来源:artifact.service.go

示例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
}
开发者ID:oblank,项目名称:rozklad_cdtu,代码行数:8,代码来源:shared.go

示例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)
}
开发者ID:AlxGolubev,项目名称:golinks,代码行数:8,代码来源:domains_controller.go

示例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)
}
开发者ID:danihodovic,项目名称:backend-test,代码行数:9,代码来源:routes.go

示例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
}
开发者ID:slnaing07,项目名称:revel-modz,代码行数:8,代码来源:maillist_user.go

示例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
}
开发者ID:jwmiller19,项目名称:revel-modz,代码行数:8,代码来源:maillist_public.go

示例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
}
开发者ID:slnaing07,项目名称:revel-modz,代码行数:8,代码来源:analytics_public.go

示例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
}
开发者ID:sirlori,项目名称:contrib,代码行数:10,代码来源:resourcer.go

示例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)
	})
}
开发者ID:ali-abdalla,项目名称:humhub-api,代码行数:10,代码来源:like.go

示例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)
	})
}
开发者ID:ali-abdalla,项目名称:humhub-api,代码行数:10,代码来源:wall_entry.go


注:本文中的github.com/jinzhu/gorm.DB.Find方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。