本文整理汇总了Golang中github.com/jinzhu/gorm.Scope.InstanceGet方法的典型用法代码示例。如果您正苦于以下问题:Golang Scope.InstanceGet方法的具体用法?Golang Scope.InstanceGet怎么用?Golang Scope.InstanceGet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/jinzhu/gorm.Scope
的用法示例。
在下文中一共展示了Scope.InstanceGet方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: createPublishEvent
func createPublishEvent(scope *gorm.Scope) {
if _, ok := scope.InstanceGet("publish:creating_publish_event"); ok {
if event, ok := scope.Get(publishEvent); ok {
if event, ok := event.(*PublishEvent); ok {
event.PublishStatus = DIRTY
scope.Err(scope.NewDB().Save(&event).Error)
}
}
}
}
示例2: afterUpdate
func afterUpdate(scope *gorm.Scope) {
if !scope.HasError() {
if isLocalizable(scope) {
if locale, ok := getLocale(scope); ok {
if scope.DB().RowsAffected == 0 && !scope.PrimaryKeyZero() { //is locale and nothing updated
var count int
var query = fmt.Sprintf("%v.language_code = ? AND %v.%v = ?", scope.QuotedTableName(), scope.QuotedTableName(), scope.PrimaryKey())
// if enabled soft delete, delete soft deleted records
if scope.HasColumn("DeletedAt") {
scope.NewDB().Unscoped().Where("deleted_at is not null").Where(query, locale, scope.PrimaryKeyValue()).Delete(scope.Value)
}
// if no localized records exist, localize it
if scope.NewDB().Table(scope.TableName()).Where(query, locale, scope.PrimaryKeyValue()).Count(&count); count == 0 {
scope.DB().Create(scope.Value)
}
}
} else if syncColumns := syncColumns(scope); len(syncColumns) > 0 { // is global
if mode, _ := scope.DB().Get("l10n:mode"); mode != "unscoped" {
if scope.DB().RowsAffected > 0 {
var primaryField = scope.PrimaryField()
var syncAttrs = map[string]interface{}{}
if updateAttrs, ok := scope.InstanceGet("gorm:update_attrs"); ok {
for key, value := range updateAttrs.(map[string]interface{}) {
for _, syncColumn := range syncColumns {
if syncColumn == key {
syncAttrs[syncColumn] = value
break
}
}
}
} else {
var fields = scope.Fields()
for _, syncColumn := range syncColumns {
if field, ok := fields[syncColumn]; ok && field.IsNormal {
syncAttrs[syncColumn] = field.Field.Interface()
}
}
}
if len(syncAttrs) > 0 {
db := scope.DB().Model(reflect.New(utils.ModelType(scope.Value)).Interface()).Set("l10n:mode", "unscoped").Where("language_code <> ?", Global)
if !primaryField.IsBlank {
db = db.Where(fmt.Sprintf("%v = ?", primaryField.DBName), primaryField.Field.Interface())
}
scope.Err(db.UpdateColumns(syncAttrs).Error)
}
}
}
}
}
}
}
示例3: isProductionModeAndNewScope
func isProductionModeAndNewScope(scope *gorm.Scope) (isProduction bool, clone *gorm.Scope) {
if !IsDraftMode(scope.DB()) {
if _, ok := scope.InstanceGet("publish:supported_model"); ok {
table := OriginalTableName(scope.TableName())
clone := scope.New(scope.Value)
clone.Search.Table(table)
return true, clone
}
}
return false, nil
}
示例4: syncToProductionAfterUpdate
func syncToProductionAfterUpdate(scope *gorm.Scope) {
if ok, clone := getModeAndNewScope(scope); ok {
if updateAttrs, ok := scope.InstanceGet("gorm:update_attrs"); ok {
table := originalTableName(scope.TableName())
clone.Search = scope.Search
clone.Search.Table(table)
clone.InstanceSet("gorm:update_attrs", updateAttrs)
}
gorm.Update(clone)
}
}
示例5: getModeAndNewScope
func getModeAndNewScope(scope *gorm.Scope) (isProduction bool, clone *gorm.Scope) {
if draftMode, ok := scope.Get("publish:draft_mode"); !ok || !draftMode.(bool) {
if _, ok := scope.InstanceGet("publish:supported_model"); ok {
table := originalTableName(scope.TableName())
clone := scope.New(scope.Value)
clone.Search.Table(table)
return true, clone
}
}
return false, nil
}
示例6: assignUpdatedBy
func assignUpdatedBy(scope *gorm.Scope) {
if isAuditable(scope) {
if user, ok := getCurrentUser(scope); ok {
if attrs, ok := scope.InstanceGet("gorm:update_attrs"); ok {
updateAttrs := attrs.(map[string]interface{})
updateAttrs["updated_by"] = user
scope.InstanceSet("gorm:update_attrs", updateAttrs)
} else {
scope.SetColumn("UpdatedBy", user)
}
}
}
}
示例7: syncUpdateFromProductionToDraft
func syncUpdateFromProductionToDraft(scope *gorm.Scope) {
if !scope.HasError() {
if ok, clone := isProductionModeAndNewScope(scope); ok {
if updateAttrs, ok := scope.InstanceGet("gorm:update_attrs"); ok {
table := OriginalTableName(scope.TableName())
clone.Search = scope.Search
clone.Search.Table(table)
clone.InstanceSet("gorm:update_attrs", updateAttrs)
}
scope.DB().Callback().Update().Get("gorm:update")(clone)
}
}
}
示例8: deleteScope
func deleteScope(scope *gorm.Scope) {
if !scope.HasError() {
_, supportedModel := scope.InstanceGet("publish:supported_model")
if supportedModel && IsDraftMode(scope.DB()) {
scope.Raw(
fmt.Sprintf("UPDATE %v SET deleted_at=%v, publish_status=%v %v",
scope.QuotedTableName(),
scope.AddToVars(gorm.NowFunc()),
scope.AddToVars(DIRTY),
scope.CombinedConditionSql(),
))
scope.Exec()
} else {
gorm.Delete(scope)
}
}
}
示例9: assignUpdatedBy
func assignUpdatedBy(scope *gorm.Scope) {
if isAuditable(scope) {
if user, ok := scope.DB().Get("audited:current_user"); ok {
var currentUser string
if primaryField := scope.New(user).PrimaryField(); primaryField != nil {
currentUser = fmt.Sprintf("%v", primaryField.Field.Interface())
} else {
currentUser = fmt.Sprintf("%v", user)
}
if attrs, ok := scope.InstanceGet("gorm:update_attrs"); ok {
updateAttrs := attrs.(map[string]interface{})
updateAttrs["updated_by"] = currentUser
scope.InstanceSet("gorm:update_attrs", updateAttrs)
} else {
scope.SetColumn("UpdatedBy", currentUser)
}
}
}
}
示例10: afterUpdate
func afterUpdate(scope *gorm.Scope) {
if !scope.HasError() {
if isLocalizable(scope) {
if locale, ok := getLocale(scope); ok {
if scope.DB().RowsAffected == 0 && !scope.PrimaryKeyZero() { //is locale and nothing updated
var count int
var query = fmt.Sprintf("%v.language_code = ? AND %v.%v = ?", scope.QuotedTableName(), scope.QuotedTableName(), scope.PrimaryKey())
if scope.NewDB().Table(scope.TableName()).Where(query, locale, scope.PrimaryKeyValue()).Count(&count); count == 0 {
scope.DB().Create(scope.Value)
}
}
} else if syncColumns := syncColumns(scope); len(syncColumns) > 0 { // is global
if mode, _ := scope.DB().Get("l10n:mode"); mode != "unscoped" {
if scope.DB().RowsAffected > 0 {
primaryKey := scope.PrimaryKeyValue()
if updateAttrs, ok := scope.InstanceGet("gorm:update_attrs"); ok {
var syncAttrs = map[string]interface{}{}
for key, value := range updateAttrs.(map[string]interface{}) {
for _, syncColumn := range syncColumns {
if syncColumn == key {
syncAttrs[syncColumn] = value
break
}
}
}
if len(syncAttrs) > 0 {
scope.DB().Model(scope.Value).Set("l10n:mode", "unscoped").Where("language_code <> ?", Global).UpdateColumns(syncAttrs)
}
} else {
scope.NewDB().Set("l10n:mode", "unscoped").Where(fmt.Sprintf("%v = ?", scope.PrimaryKey()), primaryKey).Select(syncColumns).Save(scope.Value)
}
}
}
}
}
}
}