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 != "" {
|
2015-02-23 04:40:39 +03:00
|
|
|
scope.Err(scope.SetColumn(relationship.ForeignFieldName, scope.New(value.Addr().Interface()).PrimaryKeyValue()))
|
2014-11-26 08:23:43 +03:00
|
|
|
}
|
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
|
|
|
|
2014-07-30 17:14:10 +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-02-16 11:35:26 +03:00
|
|
|
if relationship.JoinTable == "" && relationship.ForeignFieldName != "" {
|
2015-02-23 04:40:39 +03:00
|
|
|
scope.Err(newScope.SetColumn(relationship.ForeignFieldName, scope.PrimaryKeyValue()))
|
2014-07-30 17:14:10 +04:00
|
|
|
}
|
2015-02-17 15:19:47 +03:00
|
|
|
|
2015-02-18 07:30:09 +03:00
|
|
|
if relationship.PolymorphicType != "" {
|
2015-02-23 04:40:39 +03:00
|
|
|
scope.Err(newScope.SetColumn(relationship.PolymorphicType, scope.TableName()))
|
2014-11-26 08:23:43 +03:00
|
|
|
}
|
2014-01-27 04:26:59 +04:00
|
|
|
|
2014-07-30 17:14:10 +04:00
|
|
|
scope.Err(newDB.Save(elem).Error)
|
2014-07-30 10:18:15 +04:00
|
|
|
|
2015-02-17 15:19:47 +03:00
|
|
|
if joinTable := relationship.JoinTable; joinTable != "" {
|
2015-02-28 06:48:18 +03:00
|
|
|
scope.Err(scope.db.GetJoinTableHandler(joinTable).
|
|
|
|
Add(scope.NewDB(), relationship, scope.PrimaryKeyValue(), newScope.PrimaryKeyValue()))
|
2014-07-30 10:18:15 +04:00
|
|
|
}
|
|
|
|
}
|
2014-07-30 17:14:10 +04:00
|
|
|
default:
|
2015-02-17 15:19:47 +03:00
|
|
|
elem := value.Addr().Interface()
|
|
|
|
newScope := scope.New(elem)
|
|
|
|
if relationship.ForeignFieldName != "" {
|
2015-02-23 04:40:39 +03:00
|
|
|
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 != "" {
|
2015-02-23 04:40:39 +03:00
|
|
|
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
|
|
|
}
|