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


Golang DB.Model方法代码示例

本文整理汇总了Golang中github.com/jinzhu/gorm.DB.Model方法的典型用法代码示例。如果您正苦于以下问题:Golang DB.Model方法的具体用法?Golang DB.Model怎么用?Golang DB.Model使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/jinzhu/gorm.DB的用法示例。


在下文中一共展示了DB.Model方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: AfterSave

// AfterSave invokes required actions after persisting.
func (u *Repo) AfterSave(db *gorm.DB) (err error) {
	tags := Tags{}

	err = db.Model(
		u,
	).Related(
		&tags,
	).Error

	if err != nil {
		return err
	}

	for _, tag := range tags {
		err = db.Save(
			tag,
		).Error

		if err != nil {
			return err
		}
	}

	return nil
}
开发者ID:umschlag,项目名称:umschlag-api,代码行数:26,代码来源:repo.go

示例2: UpdateValue

func (pp *PurchaseProduct) UpdateValue(db *gorm.DB, value float64) error {
	pps, err := pp.Retreive(db)
	if err != nil {
		return err
	}

	if len(pps) != 1 {
		return errors.New("record not found")
	}

	*pp = pps[0]

	if err := VerifyUpdatePurchaseProduct(db, pp); err != nil {
		return err
	}

	err = db.Model(pp).UpdateColumn(PurchaseProduct{Value: value}).Error
	if err != nil {
		return err
	}

	pp.Value = value

	return nil
}
开发者ID:asvins,项目名称:warehouse,代码行数:25,代码来源:purchaseProduct.go

示例3: AfterSave

// AfterSave invokes required actions after persisting.
func (u *Registry) AfterSave(db *gorm.DB) (err error) {
	orgs := Orgs{}

	err = db.Model(
		u,
	).Related(
		&orgs,
	).Error

	if err != nil {
		return err
	}

	for _, org := range orgs {
		err = db.Save(
			org,
		).Error

		if err != nil {
			return err
		}
	}

	return nil
}
开发者ID:umschlag,项目名称:umschlag-api,代码行数:26,代码来源:registry.go

示例4: New

func New(db *gorm.DB) i18n.Backend {
	db.AutoMigrate(&Translation{})
	if err := db.Model(&Translation{}).AddUniqueIndex("idx_translations_key_with_locale", "locale", "key").Error; err != nil {
		fmt.Printf("Failed to create unique index for translations key & locale, got: %v\n", err.Error())
	}
	return &Backend{DB: db}
}
开发者ID:8legd,项目名称:qor-qor,代码行数:7,代码来源:database.go

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

示例6: UpdateQuantity

func (pp *PurchaseProduct) UpdateQuantity(db *gorm.DB, quantity int) error {
	pps, err := pp.Retreive(db)
	if err != nil {
		return err
	}

	if len(pps) != 1 {
		return errors.New("record not found")
	}

	*pp = pps[0]

	if err := VerifyUpdatePurchaseProduct(db, pp); err != nil {
		return err
	}

	err = db.Model(pp).UpdateColumn(PurchaseProduct{Quantity: quantity}).Error
	if err != nil {
		return err
	}

	pp.Quantity = quantity

	return nil
}
开发者ID:asvins,项目名称:warehouse,代码行数:25,代码来源:purchaseProduct.go

示例7: Set

func (p *SettingDao) Set(db *gorm.DB, key string, val interface{}, enc bool) error {
	dt, err := web.ToBits(val)
	if err != nil {
		db.Rollback()
		return err
	}

	if enc {
		dt, err = p.Aes.Encode(dt)
		if err != nil {
			db.Rollback()
			return err
		}
	}

	st := Setting{ID: key}
	var cn int
	db.Model(st).Count(&cn)
	if cn == 0 {
		st.Val = dt
		db.Create(&st)
	} else {
		db.Model(&st).Updates(Setting{Val: dt})
	}
	return nil
}
开发者ID:itpkg,项目名称:base,代码行数:26,代码来源:dao.go

示例8: getRole

func (p *AuthDao) getRole(db *gorm.DB, name string, resource_type string, resource_id uint) *Role {
	role := Role{}
	db.FirstOrCreate(&role, Role{Name: name, ResourceType: resource_type, ResourceID: resource_id})

	db.Model(&role).Related(&role.Permissions)
	return &role
}
开发者ID:itpkg,项目名称:base,代码行数:7,代码来源:dao.go

示例9: UpdateTask

func UpdateTask(db *gorm.DB, data models.Tasks) error {
	err := db.Model(data).Updates(data).Error
	if err != nil {
		return err
	}
	return nil
}
开发者ID:rtm7777,项目名称:rozklad_cdtu,代码行数:7,代码来源:tasks.go

示例10: AfterSave

// AfterSave invokes required actions after persisting.
func (u *Org) AfterSave(db *gorm.DB) (err error) {
	repos := Repos{}

	err = db.Model(
		u,
	).Related(
		&repos,
	).Error

	if err != nil {
		return err
	}

	for _, repo := range repos {
		err = db.Save(
			repo,
		).Error

		if err != nil {
			return err
		}
	}

	return nil
}
开发者ID:umschlag,项目名称:umschlag-api,代码行数:26,代码来源:org.go

示例11: FeedForGroupGet

func FeedForGroupGet(db gorm.DB, params martini.Params, res http.ResponseWriter) (int, string) {
	podcast := models.Group{}
	var media = []models.Media{}
	if err := db.Where(&models.Group{Slug: params["slug"]}).First(&podcast).Error; err != nil {
		res.Header().Set(ContentType, PlainType)
		return http.StatusNotFound, "Feed not found"
	}
	db.Model(&podcast).Order("created_at desc").Limit(15).Related(&media)

	tpl, err := template.New("xml_feed").Funcs(template.FuncMap{"short": ShortenString}).Parse(xml_feed_t)
	if err != nil {
		res.Header().Set(ContentType, PlainType)
		return http.StatusInternalServerError, "Generating XML failed"
	}
	data := Feed{&podcast, &media}
	buf := new(bytes.Buffer)
	err = tpl.Execute(buf, data)
	log.Println(err)
	if err != nil {
		res.Header().Set(ContentType, PlainType)
		return http.StatusInternalServerError, "Generating XML failed"
	}
	res.Header().Set(ContentType, RSSType)
	return http.StatusOK, buf.String()
}
开发者ID:RadioTerminal,项目名称:podcaster,代码行数:25,代码来源:feed.go

示例12: GetRepo

// GetRepo retrieves a specific repo from the database.
func (db *data) GetRepo(id string) (*model.Repo, *gorm.DB) {
	var (
		record = &model.Repo{}
		query  *gorm.DB
	)

	if match, _ := regexp.MatchString("^([0-9]+)$", id); match {
		val, _ := strconv.ParseInt(id, 10, 64)

		query = db.Where(
			"id = ?",
			val,
		)
	} else {
		query = db.Where(
			"slug = ?",
			id,
		)
	}

	res := query.Model(
		&record,
	).Preload(
		"Org",
	).Preload(
		"Org.Registry",
	).Preload(
		"Tags",
	).First(
		&record,
	)

	return record, res
}
开发者ID:umschlag,项目名称:umschlag-api,代码行数:35,代码来源:repo.go

示例13: saveBinary

// SaveBinary will efficently save a binary and it's associated parts.
// Having to do this for efficency is probably a good sign that I should just
// use sqlx or something.
func saveBinary(tx *gorm.DB, b *types.Binary) error {
	var txerr error
	if tx.NewRecord(b) {
		parts := b.Parts
		b.Parts = []types.Part{}
		txerr = tx.Save(b).Error
		if txerr != nil {
			return txerr
		}
		pids := make([]int64, len(parts))
		for i, p := range parts {
			pids[i] = p.ID
		}
		txerr = tx.Model(types.Part{}).Where("id IN (?)", pids).Updates(map[string]interface{}{"binary_id": b.ID}).Error
		if txerr != nil {
			return txerr
		}
	} else {
		pids := make([]int64, len(b.Parts))
		for i, p := range b.Parts {
			pids[i] = p.ID
		}
		txerr = tx.Model(types.Part{}).Where("id IN (?)", pids).Updates(map[string]interface{}{"binary_id": b.ID}).Error
		if txerr != nil {
			return txerr
		}

	}
	return nil
}
开发者ID:hobeone,项目名称:gonab,代码行数:33,代码来源:binaries.go

示例14: CreateClient

func CreateClient(db gorm.DB, options *Options) (id int64) {

	var c Client

	db.Where(
		&Client{
			Hostname:  options.Hostname,
			Interface: options.Interface,
			MacAddr:   options.InterfaceData.HardwareAddr.String(),
			Ip:        options.Ip,
		},
	).First(&c)

	if c.Id != 0 {
		db.Model(&c).Update(
			&Client{
				LastSeen: time.Now().Unix(),
			},
		)
	} else {
		c.LastSeen = time.Now().Unix()
		c.Hostname = options.Hostname
		c.MacAddr = options.InterfaceData.HardwareAddr.String()
		c.Interface = options.Interface
		c.Ip = options.Ip

		db.Table("clients").Create(&c)
	}

	options.Client = &c

	return c.Id
}
开发者ID:mehulsbhatt,项目名称:dnas,代码行数:33,代码来源:output.go

示例15: Retreive

// Retreive purchase from database
func (purch *Purchase) Retreive(db *gorm.DB) ([]Purchase, error) {
	var purchs []Purchase
	err := db.Where(purch).Find(&purchs).Error
	if err != nil {
		return nil, err
	}

	for i, p := range purchs {
		order := Order{}
		order.ID = p.OrderId

		if err := db.Model(&p).Related(&order, "OrderId").Error; err != nil {
			return nil, err
		}

		pproducts := []PurchaseProduct{}
		if err := db.Model(order).Related(&pproducts, "Pproducts").Error; err != nil {
			fmt.Println("[ERROR] ", err.Error())
			return nil, err
		}
		order.Pproducts = pproducts

		p.PurschaseOrder = order
		purchs[i] = p
	}

	return purchs, err
}
开发者ID:asvins,项目名称:warehouse,代码行数:29,代码来源:purchase.go


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