本文整理匯總了Golang中periwinkle.Tx.Model方法的典型用法代碼示例。如果您正苦於以下問題:Golang Tx.Model方法的具體用法?Golang Tx.Model怎麽用?Golang Tx.Model使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類periwinkle.Tx
的用法示例。
在下文中一共展示了Tx.Model方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Save
func (o *Group) Save(db *periwinkle.Tx) {
if o.Subscriptions != nil {
var oldSubscriptions []Subscription
db.Model(o).Related(&oldSubscriptions)
for _, oldsub := range oldSubscriptions {
match := false
for _, newsub := range o.Subscriptions {
if newsub.AddressID == oldsub.AddressID {
match = true
break
}
}
if !match {
var o UserAddress
db.First(&o, "id = ?", oldsub.AddressID)
if o.Medium != "noop" && o.Medium != "admin" {
if err := db.Where("address_id = ? AND group_id = ?", oldsub.AddressID, oldsub.GroupID).Delete(Subscription{}).Error; err != nil {
dbError(err)
}
}
}
}
}
if err := db.Save(o).Error; err != nil {
dbError(err)
}
}
示例2: GetGroupByID
func GetGroupByID(db *periwinkle.Tx, id string) *Group {
id = strings.ToLower(id)
var o Group
if result := db.First(&o, "id = ?", id); result.Error != nil {
if result.RecordNotFound() {
return nil
}
dbError(result.Error)
}
db.Model(&o).Related(&o.Subscriptions)
return &o
}
示例3: GetSubscriptions
func (u *User) GetSubscriptions(db *periwinkle.Tx) []Subscription {
db.Model(u).Related(&u.Addresses)
addressIDs := make([]int64, len(u.Addresses))
for i, address := range u.Addresses {
addressIDs[i] = address.ID
}
var subscriptions []Subscription
if len(addressIDs) > 0 {
if result := db.Where("address_id IN (?)", addressIDs).Find(&subscriptions); result.Error != nil {
if !result.RecordNotFound() {
dbError(result.Error)
}
}
} else {
subscriptions = make([]Subscription, 0)
}
return subscriptions
}
示例4: Save
func (usr *User) Save(db *periwinkle.Tx) {
usr.ID = strings.ToLower(usr.ID)
if usr.Addresses != nil {
var oldAddresses []UserAddress
db.Model(usr).Related(&oldAddresses)
deleteAddressIDs := []int64{}
for o := range oldAddresses {
oldAddr := &oldAddresses[o]
match := false
for n := range usr.Addresses {
newAddr := &usr.Addresses[n]
if newAddr.Medium == oldAddr.Medium && newAddr.Address == oldAddr.Address {
newAddr.ID = oldAddr.ID
match = true
}
}
if !match && oldAddr.Medium != "noop" && oldAddr.Medium != "admin" {
deleteAddressIDs = append(deleteAddressIDs, oldAddr.ID)
}
}
if err := db.Save(usr).Error; err != nil {
dbError(err)
}
if len(deleteAddressIDs) > 0 {
if err := db.Where("id IN (?)", deleteAddressIDs).Delete(UserAddress{}).Error; err != nil {
dbError(err)
}
}
} else {
if err := db.Save(usr).Error; err != nil {
dbError(err)
}
}
}