gorm/callback_shared.go

71 lines
2.2 KiB
Go
Raw Normal View History

2014-01-26 13:10:33 +04:00
package gorm
2015-02-28 06:48:18 +03:00
import "reflect"
2014-01-27 04:26:59 +04:00
2014-01-26 13:10:33 +04:00
func BeginTransaction(scope *Scope) {
scope.Begin()
}
func CommitOrRollbackTransaction(scope *Scope) {
scope.CommitOrRollback()
}
2014-01-26 15:34:06 +04:00
func SaveBeforeAssociations(scope *Scope) {
2014-01-27 04:26:59 +04:00
for _, field := range scope.Fields() {
2015-03-12 13:30:59 +03:00
if scope.changeableField(field) && !field.IsBlank && !field.IsIgnored {
2015-02-17 15:19:47 +03:00
if relationship := field.Relationship; relationship != nil && relationship.Kind == "belongs_to" {
2014-09-02 15:03:01 +04:00
value := field.Field
2015-02-17 15:19:47 +03:00
scope.Err(scope.NewDB().Save(value.Addr().Interface()).Error)
2015-02-16 11:35:26 +03:00
if relationship.ForeignFieldName != "" {
scope.Err(scope.SetColumn(relationship.ForeignFieldName, scope.New(value.Addr().Interface()).PrimaryKeyValue()))
}
2014-01-27 04:26:59 +04:00
}
}
}
2014-01-26 15:34:06 +04:00
}
func SaveAfterAssociations(scope *Scope) {
2014-01-27 04:26:59 +04:00
for _, field := range scope.Fields() {
2015-03-12 13:30:59 +03:00
if scope.changeableField(field) && !field.IsBlank && !field.IsIgnored {
2015-02-17 15:19:47 +03:00
if relationship := field.Relationship; relationship != nil &&
2014-07-31 07:08:26 +04:00
(relationship.Kind == "has_one" || relationship.Kind == "has_many" || relationship.Kind == "many_to_many") {
2014-09-02 15:03:01 +04:00
value := field.Field
2014-01-27 04:26:59 +04:00
switch value.Kind() {
case reflect.Slice:
for i := 0; i < value.Len(); i++ {
newDB := scope.NewDB()
elem := value.Index(i).Addr().Interface()
2015-02-17 15:19:47 +03:00
newScope := newDB.NewScope(elem)
2014-01-27 04:26:59 +04:00
2015-03-18 06:47:11 +03:00
if relationship.JoinTableHandler == nil && relationship.ForeignFieldName != "" {
scope.Err(newScope.SetColumn(relationship.ForeignFieldName, scope.PrimaryKeyValue()))
}
2015-02-17 15:19:47 +03:00
2015-02-18 07:30:09 +03:00
if relationship.PolymorphicType != "" {
scope.Err(newScope.SetColumn(relationship.PolymorphicType, scope.TableName()))
}
2014-01-27 04:26:59 +04:00
scope.Err(newDB.Save(elem).Error)
2015-03-18 06:47:11 +03:00
if joinTableHandler := relationship.JoinTableHandler; joinTableHandler != nil {
scope.Err(joinTableHandler.Add(scope.NewDB(), scope.Value, newScope.Value))
}
}
default:
2015-02-17 15:19:47 +03:00
elem := value.Addr().Interface()
newScope := scope.New(elem)
if relationship.ForeignFieldName != "" {
scope.Err(newScope.SetColumn(relationship.ForeignFieldName, scope.PrimaryKeyValue()))
2015-02-17 15:19:47 +03:00
}
2014-01-27 04:26:59 +04:00
2015-02-18 07:30:09 +03:00
if relationship.PolymorphicType != "" {
scope.Err(newScope.SetColumn(relationship.PolymorphicType, scope.TableName()))
2014-07-30 07:32:18 +04:00
}
2015-02-17 15:19:47 +03:00
scope.Err(scope.NewDB().Save(elem).Error)
2014-01-27 04:26:59 +04:00
}
}
}
}
2014-01-26 15:34:06 +04:00
}