本文整理汇总了Golang中github.com/jinzhu/gorm.DB.Preload方法的典型用法代码示例。如果您正苦于以下问题:Golang DB.Preload方法的具体用法?Golang DB.Preload怎么用?Golang DB.Preload使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/jinzhu/gorm.DB
的用法示例。
在下文中一共展示了DB.Preload方法的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: FacultiesDepartments
func FacultiesDepartments(db *gorm.DB) ([]json_models.FacultiesDepartments, error) {
var result []json_models.FacultiesDepartments
err := db.Model(&models.Faculties{}).Select("id as faculty_id, short_name as faculty_name").Scan(&result).Error
if err != nil {
return nil, err
}
for i, faculty := range result {
var records []models.Departments
err := db.Preload("Faculty").Where("faculty_id = ?", faculty.FacultyId).Find(&records).Error
if err != nil {
return nil, err
}
if len(records) != 0 {
for _, record := range records {
val := json_models.Department{Id: record.Id, Name: record.Name}
result[i].Departments = append(result[i].Departments, val)
}
} else {
result[i].Departments = []json_models.Department{}
}
}
return result, nil
}
示例3: FindServiceByObjectID
// find a service by id
func FindServiceByObjectID(db *gorm.DB, id string) (Service, bool, error) {
result := Service{}
err := db.Preload("Identity").Where(&Service{ObjectID: id}).First(&result).Error
if err != nil {
if err == gorm.RecordNotFound {
return result, false, nil
}
return result, false, err
}
return result, true, nil
}
示例4: FindObjectByObjectIDOrPin
// find object by object id or by pin
func FindObjectByObjectIDOrPin(db *gorm.DB, IDOrPin string) (Object, bool, error) {
result := Object{}
err := db.Preload("Service.Identity").Preload("Wallet.Identity").Or(&Object{ObjectID: IDOrPin}).Where(&Object{Pin: IDOrPin}).First(&result).Error
if err != nil {
if err == gorm.RecordNotFound {
return result, false, nil
}
return result, false, err
}
return result, true, nil
}
示例5: FindWalletByObjectID
// find wallet by object id
func FindWalletByObjectID(db *gorm.DB, objectID string) (Wallet, bool, error) {
result := Wallet{}
err := db.Preload("Identity").Where(&Wallet{ObjectID: objectID}).First(&result).Error
if err != nil {
if err == gorm.RecordNotFound {
return result, false, nil
}
return result, false, err
}
return result, true, nil
}
示例6: FindServiceByClientId
// find a service by it's client id
func FindServiceByClientId(db *gorm.DB, clientId string) (Service, bool, error) {
result := Service{}
err := db.Preload("Identity").Where(&Service{ClientID: clientId}).First(&result).Error
if err != nil {
if err == gorm.RecordNotFound {
return result, false, nil
} else {
return result, false, err
}
}
return result, true, nil
}
示例7: FetchUserById
func FetchUserById(id string, DB *gorm.DB) *gorm.DB {
__id, _ := utils.ParseId(id)
return DB.Preload("Account").Where(gorm.Model{ID: __id}).First(&User{})
}
示例8: Fetch
func Fetch(DB *gorm.DB) *gorm.DB {
var u []User
return DB.Preload("Account").Find(&u)
}
示例9: RefreshTokenPreloadWithPrefix
// RefreshTokenPreloadWithPrefix sets up Gorm preloads for a refresh token object,
// and prefixes with prefix for nested objects
func RefreshTokenPreloadWithPrefix(db *gorm.DB, prefix string) *gorm.DB {
return db.
Preload(prefix + "Client").Preload(prefix + "User")
}
示例10: AuthorizationCodePreloadWithPrefix
// AuthorizationCodePreloadWithPrefix sets up Gorm preloads for an auth code object,
// and prefixes with prefix for nested objects
func AuthorizationCodePreloadWithPrefix(db *gorm.DB, prefix string) *gorm.DB {
return db.
Preload(prefix + "Client").Preload(prefix + "User")
}
示例11: FindAllObjectsByObjectID
// find all objects contained in a list of object ids
func FindAllObjectsByObjectID(db *gorm.DB, objects []string) ([]Object, error) {
result := []Object{}
return result, db.Preload("Service.Identity").Preload("Wallet.Identity").Where("object_id IN (?)", objects).Find(&result).Error
}
示例12: PasswordResetPreloadWithPrefix
// PasswordResetPreloadWithPrefix sets up Gorm preloads for a password reset object,
// and prefixes with prefix for nested objects
func PasswordResetPreloadWithPrefix(db *gorm.DB, prefix string) *gorm.DB {
return db.Preload(prefix + "User.OauthUser")
}
示例13: InvitationPreloadWithPrefix
// InvitationPreloadWithPrefix sets up Gorm preloads for an invitation object,
// and prefixes with prefix for nested objects
func InvitationPreloadWithPrefix(db *gorm.DB, prefix string) *gorm.DB {
return db.Preload(prefix + "InvitedUser.OauthUser").
Preload(prefix + "InvitedByUser.OauthUser")
}
示例14: ConfirmationPreloadWithPrefix
// ConfirmationPreloadWithPrefix sets up Gorm preloads for a confirmation object,
// and prefixes with prefix for nested objects
func ConfirmationPreloadWithPrefix(db *gorm.DB, prefix string) *gorm.DB {
return db.Preload(prefix + "User.OauthUser")
}
示例15: UserPreloadWithPrefix
// UserPreloadWithPrefix sets up Gorm preloads for a user object,
// and prefixes with prefix for nested objects
func UserPreloadWithPrefix(db *gorm.DB, prefix string) *gorm.DB {
return db.Preload(prefix + "Account.OauthClient").
Preload(prefix + "OauthUser")
}