本文整理汇总了Golang中github.com/jinzhu/gorm.Scope.PrimaryKeyValue方法的典型用法代码示例。如果您正苦于以下问题:Golang Scope.PrimaryKeyValue方法的具体用法?Golang Scope.PrimaryKeyValue怎么用?Golang Scope.PrimaryKeyValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/jinzhu/gorm.Scope
的用法示例。
在下文中一共展示了Scope.PrimaryKeyValue方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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)
}
}
}
}
}
}
}
示例2: initalizePosition
func initalizePosition(scope *gorm.Scope) {
if !scope.HasError() {
if position, ok := scope.Value.(sortingInterface); ok {
if pos, err := strconv.Atoi(fmt.Sprintf("%v", scope.PrimaryKeyValue())); err == nil {
if scope.DB().UpdateColumn("position", pos).Error == nil {
position.SetPosition(pos)
}
}
}
}
}
示例3: getFuncMap
func getFuncMap(scope *gorm.Scope, field *gorm.Field, filename string) template.FuncMap {
hash := func() string { return strings.Replace(time.Now().Format("20060102150506.000000000"), ".", "", -1) }
return template.FuncMap{
"class": scope.TableName,
"primary_key": func() string { return fmt.Sprintf("%v", scope.PrimaryKeyValue()) },
"column": func() string { return field.Name },
"filename": func() string { return filename },
"basename": func() string { return strings.TrimSuffix(path.Base(filename), path.Ext(filename)) },
"hash": hash,
"filename_with_hash": func() string {
return fmt.Sprintf("%v.%v%v", strings.TrimSuffix(filename, path.Ext(filename)), hash(), path.Ext(filename))
},
"extension": func() string { return strings.TrimPrefix(path.Ext(filename), ".") },
}
}
示例4: getFuncMap
func getFuncMap(scope *gorm.Scope, field *gorm.Field, filename string) template.FuncMap {
hash := func() string { return strings.Replace(time.Now().Format("20060102150506.000000000"), ".", "", -1) }
return template.FuncMap{
"class": func() string { return inflection.Plural(utils.ToParamString(scope.GetModelStruct().ModelType.Name())) },
"primary_key": func() string { return fmt.Sprintf("%v", scope.PrimaryKeyValue()) },
"column": func() string { return strings.ToLower(field.Name) },
"filename": func() string { return filename },
"basename": func() string { return strings.TrimSuffix(path.Base(filename), path.Ext(filename)) },
"hash": hash,
"filename_with_hash": func() string {
return urlReplacer.ReplaceAllString(fmt.Sprintf("%v.%v%v", strings.TrimSuffix(filename, path.Ext(filename)), hash(), path.Ext(filename)), "-")
},
"extension": func() string { return strings.TrimPrefix(path.Ext(filename), ".") },
}
}
示例5: Stringify
func Stringify(object interface{}) string {
if obj, ok := object.(interface {
Stringify() string
}); ok {
return obj.Stringify()
}
scope := gorm.Scope{Value: object}
for _, column := range []string{"Name", "Title"} {
if field, ok := scope.FieldByName(column); ok {
return fmt.Sprintf("%v", field.Field.Interface())
}
}
if scope.PrimaryKeyZero() {
return ""
} else {
return fmt.Sprintf("%v#%v", scope.GetModelStruct().ModelType.Name(), scope.PrimaryKeyValue())
}
}
示例6: 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)
}
}
}
}
}
}
}
示例7: stringify
func stringify(object interface{}) string {
if obj, ok := object.(interface {
Stringify() string
}); ok {
return obj.Stringify()
}
scope := gorm.Scope{Value: object}
for _, column := range []string{"Description", "Name", "Title", "Code"} {
if field, ok := scope.FieldByName(column); ok {
return fmt.Sprintf("%v", field.Field.Interface())
}
}
if scope.PrimaryField() != nil {
if scope.PrimaryKeyZero() {
return ""
}
return fmt.Sprintf("%v#%v", scope.GetModelStruct().ModelType.Name(), scope.PrimaryKeyValue())
}
return fmt.Sprint(reflect.Indirect(reflect.ValueOf(object)).Interface())
}
示例8: Label
// Label is a label including model type, primary key and column name
func (err Error) Label() string {
scope := gorm.Scope{Value: err.Resource}
return fmt.Sprintf("%v_%v_%v", scope.GetModelStruct().ModelType.Name(), scope.PrimaryKeyValue(), err.Column)
}