Remove unnecessary saving SQL when Append new association

This commit is contained in:
Jinzhu 2015-12-26 19:49:31 +08:00
parent e8fe7c5e34
commit c087e6dcc6
1 changed files with 17 additions and 4 deletions

View File

@ -39,21 +39,28 @@ func (association *Association) saveAssociations(values ...interface{}) *Associa
reflectValue = reflectPtr reflectValue = reflectPtr
} }
// value has to been saved // value has to been saved for many2many
if scope.New(reflectValue.Interface()).PrimaryKeyZero() { if relationship.Kind == "many_to_many" {
scope.NewDB().Save(reflectValue.Interface()) if scope.New(reflectValue.Interface()).PrimaryKeyZero() {
scope.NewDB().Save(reflectValue.Interface())
}
} }
// Assign Fields // Assign Fields
fieldType := field.Field.Type() var fieldType = field.Field.Type()
var setFieldBackToValue, setSliceFieldBackToValue bool
if reflectValue.Type().AssignableTo(fieldType) { if reflectValue.Type().AssignableTo(fieldType) {
field.Set(reflectValue) field.Set(reflectValue)
} else if reflectValue.Type().Elem().AssignableTo(fieldType) { } else if reflectValue.Type().Elem().AssignableTo(fieldType) {
// if field's type is struct, then need to set value back to argument after save
setFieldBackToValue = true
field.Set(reflectValue.Elem()) field.Set(reflectValue.Elem())
} else if fieldType.Kind() == reflect.Slice { } else if fieldType.Kind() == reflect.Slice {
if reflectValue.Type().AssignableTo(fieldType.Elem()) { if reflectValue.Type().AssignableTo(fieldType.Elem()) {
field.Set(reflect.Append(field.Field, reflectValue)) field.Set(reflect.Append(field.Field, reflectValue))
} else if reflectValue.Type().Elem().AssignableTo(fieldType.Elem()) { } else if reflectValue.Type().Elem().AssignableTo(fieldType.Elem()) {
// if field's type is slice of struct, then need to set value back to argument after save
setSliceFieldBackToValue = true
field.Set(reflect.Append(field.Field, reflectValue.Elem())) field.Set(reflect.Append(field.Field, reflectValue.Elem()))
} }
} }
@ -62,6 +69,12 @@ func (association *Association) saveAssociations(values ...interface{}) *Associa
association.setErr(relationship.JoinTableHandler.Add(relationship.JoinTableHandler, scope.NewDB(), scope.Value, reflectValue.Interface())) association.setErr(relationship.JoinTableHandler.Add(relationship.JoinTableHandler, scope.NewDB(), scope.Value, reflectValue.Interface()))
} else { } else {
association.setErr(scope.NewDB().Select(field.Name).Save(scope.Value).Error) association.setErr(scope.NewDB().Select(field.Name).Save(scope.Value).Error)
if setFieldBackToValue {
reflectValue.Elem().Set(field.Field)
} else if setSliceFieldBackToValue {
reflectValue.Elem().Set(field.Field.Index(field.Field.Len() - 1))
}
} }
} }