forked from mirror/gorm
Replace BeforeAssociation, AfterAssociation with association type
This commit is contained in:
parent
2356182ea7
commit
6c4b635176
|
@ -16,7 +16,9 @@ func CommitOrRollbackTransaction(scope *Scope) {
|
|||
|
||||
func SaveBeforeAssociations(scope *Scope) {
|
||||
for _, field := range scope.Fields() {
|
||||
if field.BeforeAssociation && !field.IsBlank && !field.IsIgnored {
|
||||
if !field.IsBlank && !field.IsIgnored {
|
||||
relationship := field.Relationship
|
||||
if relationship != nil && relationship.kind == "belongs_to" {
|
||||
value := reflect.ValueOf(field.Value)
|
||||
newDB := scope.NewDB()
|
||||
|
||||
|
@ -32,8 +34,9 @@ func SaveBeforeAssociations(scope *Scope) {
|
|||
scope.SetColumn(field.Name, value.Interface())
|
||||
}
|
||||
|
||||
if field.Relationship != nil && field.Relationship.foreignKey != "" {
|
||||
scope.SetColumn(field.Relationship.foreignKey, newDB.NewScope(value.Interface()).PrimaryKeyValue())
|
||||
if relationship.foreignKey != "" {
|
||||
scope.SetColumn(relationship.foreignKey, newDB.NewScope(value.Interface()).PrimaryKeyValue())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -41,7 +44,10 @@ func SaveBeforeAssociations(scope *Scope) {
|
|||
|
||||
func SaveAfterAssociations(scope *Scope) {
|
||||
for _, field := range scope.Fields() {
|
||||
if field.AfterAssociation && !field.IsBlank && !field.IsIgnored {
|
||||
if !field.IsBlank && !field.IsIgnored {
|
||||
relationship := field.Relationship
|
||||
if relationship != nil &&
|
||||
(relationship.kind == "has_one" || relationship.kind == "has_many" || relationship.kind == "many_to_many") {
|
||||
value := reflect.ValueOf(field.Value)
|
||||
|
||||
switch value.Kind() {
|
||||
|
@ -50,18 +56,18 @@ func SaveAfterAssociations(scope *Scope) {
|
|||
newDB := scope.NewDB()
|
||||
elem := value.Index(i).Addr().Interface()
|
||||
|
||||
if field.Relationship != nil && field.Relationship.joinTable == "" && field.Relationship.foreignKey != "" {
|
||||
newDB.NewScope(elem).SetColumn(field.Relationship.foreignKey, scope.PrimaryKeyValue())
|
||||
if relationship.joinTable == "" && relationship.foreignKey != "" {
|
||||
newDB.NewScope(elem).SetColumn(relationship.foreignKey, scope.PrimaryKeyValue())
|
||||
}
|
||||
|
||||
scope.Err(newDB.Save(elem).Error)
|
||||
|
||||
if field.Relationship != nil && field.Relationship.joinTable != "" {
|
||||
if relationship.joinTable != "" {
|
||||
newScope := scope.New(elem)
|
||||
joinTable := field.Relationship.joinTable
|
||||
foreignKey := ToSnake(field.Relationship.foreignKey)
|
||||
joinTable := relationship.joinTable
|
||||
foreignKey := ToSnake(relationship.foreignKey)
|
||||
foreignValue := fmt.Sprintf("%v", scope.PrimaryKeyValue())
|
||||
associationForeignKey := ToSnake(field.Relationship.associationForeignKey)
|
||||
associationForeignKey := ToSnake(relationship.associationForeignKey)
|
||||
associationForeignValue := fmt.Sprintf("%v", newScope.PrimaryKeyValue())
|
||||
|
||||
newScope.Raw(fmt.Sprintf(
|
||||
|
@ -84,8 +90,8 @@ func SaveAfterAssociations(scope *Scope) {
|
|||
default:
|
||||
newDB := scope.NewDB()
|
||||
if value.CanAddr() {
|
||||
if field.Relationship != nil {
|
||||
newDB.NewScope(field.Value).SetColumn(field.Relationship.foreignKey, scope.PrimaryKeyValue())
|
||||
if relationship.foreignKey != "" {
|
||||
newDB.NewScope(field.Value).SetColumn(relationship.foreignKey, scope.PrimaryKeyValue())
|
||||
}
|
||||
scope.Err(newDB.Save(field.Value).Error)
|
||||
} else {
|
||||
|
@ -96,8 +102,8 @@ func SaveAfterAssociations(scope *Scope) {
|
|||
}
|
||||
|
||||
elem := destValue.Addr().Interface()
|
||||
if field.Relationship != nil {
|
||||
newDB.NewScope(elem).SetColumn(field.Relationship.foreignKey, scope.PrimaryKeyValue())
|
||||
if relationship.foreignKey != "" {
|
||||
newDB.NewScope(elem).SetColumn(relationship.foreignKey, scope.PrimaryKeyValue())
|
||||
}
|
||||
scope.Err(newDB.Save(elem).Error)
|
||||
scope.SetColumn(field.Name, destValue.Interface())
|
||||
|
@ -105,4 +111,5 @@ func SaveAfterAssociations(scope *Scope) {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
3
field.go
3
field.go
|
@ -10,6 +10,7 @@ type relationship struct {
|
|||
joinTable string
|
||||
foreignKey string
|
||||
associationForeignKey string
|
||||
kind string
|
||||
}
|
||||
|
||||
type Field struct {
|
||||
|
@ -20,8 +21,6 @@ type Field struct {
|
|||
IsIgnored bool
|
||||
Tag reflect.StructTag
|
||||
SqlTag string
|
||||
BeforeAssociation bool
|
||||
AfterAssociation bool
|
||||
isPrimaryKey bool
|
||||
Relationship *relationship
|
||||
}
|
||||
|
|
15
scope.go
15
scope.go
|
@ -284,29 +284,30 @@ func (scope *Scope) fieldFromStruct(fieldStruct reflect.StructField) *Field {
|
|||
}
|
||||
}
|
||||
|
||||
field.AfterAssociation = true
|
||||
field.Relationship = &relationship{
|
||||
joinTable: many2many,
|
||||
foreignKey: foreignKey,
|
||||
associationForeignKey: associationForeignKey,
|
||||
kind: "has_many",
|
||||
}
|
||||
|
||||
if many2many != "" {
|
||||
field.Relationship.kind = "many_to_many"
|
||||
}
|
||||
}
|
||||
case reflect.Struct:
|
||||
if !field.IsTime() && !field.IsScanner() {
|
||||
if foreignKey == "" && scope.HasColumn(field.Name+"Id") {
|
||||
field.Relationship = &relationship{foreignKey: field.Name + "Id"}
|
||||
field.BeforeAssociation = true
|
||||
field.Relationship = &relationship{foreignKey: field.Name + "Id", kind: "belongs_to"}
|
||||
} else if scope.HasColumn(foreignKey) {
|
||||
field.Relationship = &relationship{foreignKey: foreignKey}
|
||||
field.BeforeAssociation = true
|
||||
field.Relationship = &relationship{foreignKey: foreignKey, kind: "belongs_to"}
|
||||
} else {
|
||||
if foreignKey == "" {
|
||||
foreignKey = scopeTyp.Name() + "Id"
|
||||
}
|
||||
if reflect.New(typ).Elem().FieldByName(foreignKey).IsValid() {
|
||||
field.Relationship = &relationship{foreignKey: foreignKey}
|
||||
field.Relationship = &relationship{foreignKey: foreignKey, kind: "has_one"}
|
||||
}
|
||||
field.AfterAssociation = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue