本文整理匯總了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
}