本文整理汇总了Golang中github.com/jinzhu/gorm.Scope.New方法的典型用法代码示例。如果您正苦于以下问题:Golang Scope.New方法的具体用法?Golang Scope.New怎么用?Golang Scope.New使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/jinzhu/gorm.Scope
的用法示例。
在下文中一共展示了Scope.New方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: 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
}
示例3: updateFields
// Callback function: invoked after the creation/update of an object. To populate its default fields
func updateFields(scope *gorm.Scope) {
if !scope.HasError() {
newScope := scope.New(scope.Value)
newScope.Search = newScope.Search.Table(scope.TableName())
if scope.PrimaryKey() != "" {
gorm.Query(newScope)
} else {
// TODO: find a way to populate fields of scope.Value selecting * matching on every fields
}
scope = newScope
}
}
示例4: assignCreatedBy
func assignCreatedBy(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)
}
scope.SetColumn("CreatedBy", currentUser)
}
}
}
示例5: 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)
}
}
}
}
示例6: getCurrentUser
func getCurrentUser(scope *gorm.Scope) (string, bool) {
var user interface{}
var hasUser bool
user, hasUser = scope.DB().Get("qor:current_user")
if !hasUser {
user, hasUser = scope.DB().Get("audited:current_user")
}
if hasUser {
var currentUser string
if primaryField := scope.New(user).PrimaryField(); primaryField != nil {
currentUser = fmt.Sprintf("%v", primaryField.Field.Interface())
} else {
currentUser = fmt.Sprintf("%v", user)
}
return currentUser, true
}
return "", false
}