callback_save.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package gorm
  2. import "reflect"
  3. func beginTransactionCallback(scope *Scope) {
  4. scope.Begin()
  5. }
  6. func commitOrRollbackTransactionCallback(scope *Scope) {
  7. scope.CommitOrRollback()
  8. }
  9. func saveFieldAsAssociation(scope *Scope, field *Field) (bool, *Relationship) {
  10. if scope.changeableField(field) && !field.IsBlank && !field.IsIgnored {
  11. if value, ok := field.TagSettings["SAVE_ASSOCIATIONS"]; !ok || (value != "false" && value != "skip") {
  12. if relationship := field.Relationship; relationship != nil {
  13. return true, relationship
  14. }
  15. }
  16. }
  17. return false, nil
  18. }
  19. func saveBeforeAssociationsCallback(scope *Scope) {
  20. if !scope.shouldSaveAssociations() {
  21. return
  22. }
  23. for _, field := range scope.Fields() {
  24. if ok, relationship := saveFieldAsAssociation(scope, field); ok && relationship.Kind == "belongs_to" {
  25. fieldValue := field.Field.Addr().Interface()
  26. scope.Err(scope.NewDB().Save(fieldValue).Error)
  27. if len(relationship.ForeignFieldNames) != 0 {
  28. // set value's foreign key
  29. for idx, fieldName := range relationship.ForeignFieldNames {
  30. associationForeignName := relationship.AssociationForeignDBNames[idx]
  31. if foreignField, ok := scope.New(fieldValue).FieldByName(associationForeignName); ok {
  32. scope.Err(scope.SetColumn(fieldName, foreignField.Field.Interface()))
  33. }
  34. }
  35. }
  36. }
  37. }
  38. }
  39. func saveAfterAssociationsCallback(scope *Scope) {
  40. if !scope.shouldSaveAssociations() {
  41. return
  42. }
  43. for _, field := range scope.Fields() {
  44. if ok, relationship := saveFieldAsAssociation(scope, field); ok &&
  45. (relationship.Kind == "has_one" || relationship.Kind == "has_many" || relationship.Kind == "many_to_many") {
  46. value := field.Field
  47. switch value.Kind() {
  48. case reflect.Slice:
  49. for i := 0; i < value.Len(); i++ {
  50. newDB := scope.NewDB()
  51. elem := value.Index(i).Addr().Interface()
  52. newScope := newDB.NewScope(elem)
  53. if relationship.JoinTableHandler == nil && len(relationship.ForeignFieldNames) != 0 {
  54. for idx, fieldName := range relationship.ForeignFieldNames {
  55. associationForeignName := relationship.AssociationForeignDBNames[idx]
  56. if f, ok := scope.FieldByName(associationForeignName); ok {
  57. scope.Err(newScope.SetColumn(fieldName, f.Field.Interface()))
  58. }
  59. }
  60. }
  61. if relationship.PolymorphicType != "" {
  62. scope.Err(newScope.SetColumn(relationship.PolymorphicType, relationship.PolymorphicValue))
  63. }
  64. scope.Err(newDB.Save(elem).Error)
  65. if joinTableHandler := relationship.JoinTableHandler; joinTableHandler != nil {
  66. scope.Err(joinTableHandler.Add(joinTableHandler, newDB, scope.Value, newScope.Value))
  67. }
  68. }
  69. default:
  70. elem := value.Addr().Interface()
  71. newScope := scope.New(elem)
  72. if len(relationship.ForeignFieldNames) != 0 {
  73. for idx, fieldName := range relationship.ForeignFieldNames {
  74. associationForeignName := relationship.AssociationForeignDBNames[idx]
  75. if f, ok := scope.FieldByName(associationForeignName); ok {
  76. scope.Err(newScope.SetColumn(fieldName, f.Field.Interface()))
  77. }
  78. }
  79. }
  80. if relationship.PolymorphicType != "" {
  81. scope.Err(newScope.SetColumn(relationship.PolymorphicType, relationship.PolymorphicValue))
  82. }
  83. scope.Err(scope.NewDB().Save(elem).Error)
  84. }
  85. }
  86. }
  87. }