Allow save association relations w/o saving association

This commit is contained in:
Jinzhu 2018-02-10 15:16:20 +08:00
parent 63cb513b49
commit 43dc867644
3 changed files with 20 additions and 15 deletions

View File

@ -909,7 +909,7 @@ func TestSkipSaveAssociation(t *testing.T) {
company := Company{Name: "skip_save_association"} company := Company{Name: "skip_save_association"}
DB.Save(&company) DB.Save(&company)
company.Name = "skip_save_association_modified" company.Name = "skip_save_association_modified"
user := User{Name: "jinzhu", CompanyID: company.ID, Company: company} user := User{Name: "jinzhu", Company: company}
DB.Save(&user) DB.Save(&user)
if !DB.Where("name = ?", "skip_save_association_modified").First(&Company{}).RecordNotFound() { if !DB.Where("name = ?", "skip_save_association_modified").First(&Company{}).RecordNotFound() {

View File

@ -12,22 +12,25 @@ func commitOrRollbackTransactionCallback(scope *Scope) {
func saveFieldAsAssociation(scope *Scope, field *Field) (bool, *Relationship) { func saveFieldAsAssociation(scope *Scope, field *Field) (bool, *Relationship) {
if scope.changeableField(field) && !field.IsBlank && !field.IsIgnored { if scope.changeableField(field) && !field.IsBlank && !field.IsIgnored {
if value, ok := field.TagSettings["SAVE_ASSOCIATIONS"]; !ok || (value != "false" && value != "skip") { if field.Relationship != nil {
if value, ok := field.TagSettings["SAVE_ASSOCIATIONS"]; (!ok || (value != "false" && value != "skip")) && scope.allowSaveAssociations() {
return true, field.Relationship return true, field.Relationship
} }
return false, field.Relationship return false, field.Relationship
} }
}
return false, nil return false, nil
} }
func saveBeforeAssociationsCallback(scope *Scope) { func saveBeforeAssociationsCallback(scope *Scope) {
for _, field := range scope.Fields() { for _, field := range scope.Fields() {
ok, relationship := saveFieldAsAssociation(scope, field) if allowSaveAssociation, relationship := saveFieldAsAssociation(scope, field); relationship != nil && relationship.Kind == "belongs_to" {
if relationship != nil && relationship.Kind == "belongs_to" {
fieldValue := field.Field.Addr().Interface() fieldValue := field.Field.Addr().Interface()
if ok && scope.shouldSaveAssociations() {
if allowSaveAssociation {
scope.Err(scope.NewDB().Save(fieldValue).Error) scope.Err(scope.NewDB().Save(fieldValue).Error)
} }
if len(relationship.ForeignFieldNames) != 0 { if len(relationship.ForeignFieldNames) != 0 {
// set value's foreign key // set value's foreign key
for idx, fieldName := range relationship.ForeignFieldNames { for idx, fieldName := range relationship.ForeignFieldNames {
@ -42,11 +45,8 @@ func saveBeforeAssociationsCallback(scope *Scope) {
} }
func saveAfterAssociationsCallback(scope *Scope) { func saveAfterAssociationsCallback(scope *Scope) {
if !scope.shouldSaveAssociations() {
return
}
for _, field := range scope.Fields() { for _, field := range scope.Fields() {
if ok, relationship := saveFieldAsAssociation(scope, field); ok && relationship != nil && if allowSaveAssociation, relationship := saveFieldAsAssociation(scope, field); relationship != nil &&
(relationship.Kind == "has_one" || relationship.Kind == "has_many" || relationship.Kind == "many_to_many") { (relationship.Kind == "has_one" || relationship.Kind == "has_many" || relationship.Kind == "many_to_many") {
value := field.Field value := field.Field
@ -70,9 +70,11 @@ func saveAfterAssociationsCallback(scope *Scope) {
scope.Err(newScope.SetColumn(relationship.PolymorphicType, relationship.PolymorphicValue)) scope.Err(newScope.SetColumn(relationship.PolymorphicType, relationship.PolymorphicValue))
} }
if allowSaveAssociation {
scope.Err(newDB.Save(elem).Error) scope.Err(newDB.Save(elem).Error)
}
if joinTableHandler := relationship.JoinTableHandler; joinTableHandler != nil { if joinTableHandler := relationship.JoinTableHandler; joinTableHandler != nil && !newScope.PrimaryKeyZero() {
scope.Err(joinTableHandler.Add(joinTableHandler, newDB, scope.Value, newScope.Value)) scope.Err(joinTableHandler.Add(joinTableHandler, newDB, scope.Value, newScope.Value))
} }
} }
@ -91,8 +93,11 @@ func saveAfterAssociationsCallback(scope *Scope) {
if relationship.PolymorphicType != "" { if relationship.PolymorphicType != "" {
scope.Err(newScope.SetColumn(relationship.PolymorphicType, relationship.PolymorphicValue)) scope.Err(newScope.SetColumn(relationship.PolymorphicType, relationship.PolymorphicValue))
} }
if allowSaveAssociation {
scope.Err(scope.NewDB().Save(elem).Error) scope.Err(scope.NewDB().Save(elem).Error)
} }
} }
} }
} }
}

View File

@ -993,7 +993,7 @@ func (scope *Scope) changeableField(field *Field) bool {
return true return true
} }
func (scope *Scope) shouldSaveAssociations() bool { func (scope *Scope) allowSaveAssociations() bool {
if saveAssociations, ok := scope.Get("gorm:save_associations"); ok { if saveAssociations, ok := scope.Get("gorm:save_associations"); ok {
if v, ok := saveAssociations.(bool); ok && !v { if v, ok := saveAssociations.(bool); ok && !v {
return false return false