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) {
|
func SaveBeforeAssociations(scope *Scope) {
|
||||||
for _, field := range scope.Fields() {
|
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)
|
value := reflect.ValueOf(field.Value)
|
||||||
newDB := scope.NewDB()
|
newDB := scope.NewDB()
|
||||||
|
|
||||||
|
@ -32,8 +34,9 @@ func SaveBeforeAssociations(scope *Scope) {
|
||||||
scope.SetColumn(field.Name, value.Interface())
|
scope.SetColumn(field.Name, value.Interface())
|
||||||
}
|
}
|
||||||
|
|
||||||
if field.Relationship != nil && field.Relationship.foreignKey != "" {
|
if relationship.foreignKey != "" {
|
||||||
scope.SetColumn(field.Relationship.foreignKey, newDB.NewScope(value.Interface()).PrimaryKeyValue())
|
scope.SetColumn(relationship.foreignKey, newDB.NewScope(value.Interface()).PrimaryKeyValue())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,7 +44,10 @@ func SaveBeforeAssociations(scope *Scope) {
|
||||||
|
|
||||||
func SaveAfterAssociations(scope *Scope) {
|
func SaveAfterAssociations(scope *Scope) {
|
||||||
for _, field := range scope.Fields() {
|
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)
|
value := reflect.ValueOf(field.Value)
|
||||||
|
|
||||||
switch value.Kind() {
|
switch value.Kind() {
|
||||||
|
@ -50,18 +56,18 @@ func SaveAfterAssociations(scope *Scope) {
|
||||||
newDB := scope.NewDB()
|
newDB := scope.NewDB()
|
||||||
elem := value.Index(i).Addr().Interface()
|
elem := value.Index(i).Addr().Interface()
|
||||||
|
|
||||||
if field.Relationship != nil && field.Relationship.joinTable == "" && field.Relationship.foreignKey != "" {
|
if relationship.joinTable == "" && relationship.foreignKey != "" {
|
||||||
newDB.NewScope(elem).SetColumn(field.Relationship.foreignKey, scope.PrimaryKeyValue())
|
newDB.NewScope(elem).SetColumn(relationship.foreignKey, scope.PrimaryKeyValue())
|
||||||
}
|
}
|
||||||
|
|
||||||
scope.Err(newDB.Save(elem).Error)
|
scope.Err(newDB.Save(elem).Error)
|
||||||
|
|
||||||
if field.Relationship != nil && field.Relationship.joinTable != "" {
|
if relationship.joinTable != "" {
|
||||||
newScope := scope.New(elem)
|
newScope := scope.New(elem)
|
||||||
joinTable := field.Relationship.joinTable
|
joinTable := relationship.joinTable
|
||||||
foreignKey := ToSnake(field.Relationship.foreignKey)
|
foreignKey := ToSnake(relationship.foreignKey)
|
||||||
foreignValue := fmt.Sprintf("%v", scope.PrimaryKeyValue())
|
foreignValue := fmt.Sprintf("%v", scope.PrimaryKeyValue())
|
||||||
associationForeignKey := ToSnake(field.Relationship.associationForeignKey)
|
associationForeignKey := ToSnake(relationship.associationForeignKey)
|
||||||
associationForeignValue := fmt.Sprintf("%v", newScope.PrimaryKeyValue())
|
associationForeignValue := fmt.Sprintf("%v", newScope.PrimaryKeyValue())
|
||||||
|
|
||||||
newScope.Raw(fmt.Sprintf(
|
newScope.Raw(fmt.Sprintf(
|
||||||
|
@ -84,8 +90,8 @@ func SaveAfterAssociations(scope *Scope) {
|
||||||
default:
|
default:
|
||||||
newDB := scope.NewDB()
|
newDB := scope.NewDB()
|
||||||
if value.CanAddr() {
|
if value.CanAddr() {
|
||||||
if field.Relationship != nil {
|
if relationship.foreignKey != "" {
|
||||||
newDB.NewScope(field.Value).SetColumn(field.Relationship.foreignKey, scope.PrimaryKeyValue())
|
newDB.NewScope(field.Value).SetColumn(relationship.foreignKey, scope.PrimaryKeyValue())
|
||||||
}
|
}
|
||||||
scope.Err(newDB.Save(field.Value).Error)
|
scope.Err(newDB.Save(field.Value).Error)
|
||||||
} else {
|
} else {
|
||||||
|
@ -96,8 +102,8 @@ func SaveAfterAssociations(scope *Scope) {
|
||||||
}
|
}
|
||||||
|
|
||||||
elem := destValue.Addr().Interface()
|
elem := destValue.Addr().Interface()
|
||||||
if field.Relationship != nil {
|
if relationship.foreignKey != "" {
|
||||||
newDB.NewScope(elem).SetColumn(field.Relationship.foreignKey, scope.PrimaryKeyValue())
|
newDB.NewScope(elem).SetColumn(relationship.foreignKey, scope.PrimaryKeyValue())
|
||||||
}
|
}
|
||||||
scope.Err(newDB.Save(elem).Error)
|
scope.Err(newDB.Save(elem).Error)
|
||||||
scope.SetColumn(field.Name, destValue.Interface())
|
scope.SetColumn(field.Name, destValue.Interface())
|
||||||
|
@ -106,3 +112,4 @@ func SaveAfterAssociations(scope *Scope) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
3
field.go
3
field.go
|
@ -10,6 +10,7 @@ type relationship struct {
|
||||||
joinTable string
|
joinTable string
|
||||||
foreignKey string
|
foreignKey string
|
||||||
associationForeignKey string
|
associationForeignKey string
|
||||||
|
kind string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Field struct {
|
type Field struct {
|
||||||
|
@ -20,8 +21,6 @@ type Field struct {
|
||||||
IsIgnored bool
|
IsIgnored bool
|
||||||
Tag reflect.StructTag
|
Tag reflect.StructTag
|
||||||
SqlTag string
|
SqlTag string
|
||||||
BeforeAssociation bool
|
|
||||||
AfterAssociation bool
|
|
||||||
isPrimaryKey bool
|
isPrimaryKey bool
|
||||||
Relationship *relationship
|
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{
|
field.Relationship = &relationship{
|
||||||
joinTable: many2many,
|
joinTable: many2many,
|
||||||
foreignKey: foreignKey,
|
foreignKey: foreignKey,
|
||||||
associationForeignKey: associationForeignKey,
|
associationForeignKey: associationForeignKey,
|
||||||
|
kind: "has_many",
|
||||||
|
}
|
||||||
|
|
||||||
|
if many2many != "" {
|
||||||
|
field.Relationship.kind = "many_to_many"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
if !field.IsTime() && !field.IsScanner() {
|
if !field.IsTime() && !field.IsScanner() {
|
||||||
if foreignKey == "" && scope.HasColumn(field.Name+"Id") {
|
if foreignKey == "" && scope.HasColumn(field.Name+"Id") {
|
||||||
field.Relationship = &relationship{foreignKey: field.Name + "Id"}
|
field.Relationship = &relationship{foreignKey: field.Name + "Id", kind: "belongs_to"}
|
||||||
field.BeforeAssociation = true
|
|
||||||
} else if scope.HasColumn(foreignKey) {
|
} else if scope.HasColumn(foreignKey) {
|
||||||
field.Relationship = &relationship{foreignKey: foreignKey}
|
field.Relationship = &relationship{foreignKey: foreignKey, kind: "belongs_to"}
|
||||||
field.BeforeAssociation = true
|
|
||||||
} else {
|
} else {
|
||||||
if foreignKey == "" {
|
if foreignKey == "" {
|
||||||
foreignKey = scopeTyp.Name() + "Id"
|
foreignKey = scopeTyp.Name() + "Id"
|
||||||
}
|
}
|
||||||
if reflect.New(typ).Elem().FieldByName(foreignKey).IsValid() {
|
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