forked from mirror/gorm
Fix pollute model's fields with join table's values
This commit is contained in:
parent
4c1a78bab7
commit
807ed63cfe
|
@ -209,6 +209,7 @@ func (scope *Scope) handleManyToManyPreload(field *Field, conditions []interface
|
||||||
}
|
}
|
||||||
|
|
||||||
db := scope.NewDB().Table(scope.New(reflect.New(destType).Interface()).TableName()).Select("*")
|
db := scope.NewDB().Table(scope.New(reflect.New(destType).Interface()).TableName()).Select("*")
|
||||||
|
|
||||||
preloadJoinDB := joinTableHandler.JoinWith(joinTableHandler, db, scope.Value)
|
preloadJoinDB := joinTableHandler.JoinWith(joinTableHandler, db, scope.Value)
|
||||||
|
|
||||||
if len(conditions) > 0 {
|
if len(conditions) > 0 {
|
||||||
|
@ -228,13 +229,15 @@ func (scope *Scope) handleManyToManyPreload(field *Field, conditions []interface
|
||||||
|
|
||||||
fields := scope.New(elem.Addr().Interface()).Fields()
|
fields := scope.New(elem.Addr().Interface()).Fields()
|
||||||
|
|
||||||
|
var foundFields = map[string]bool{}
|
||||||
for index, column := range columns {
|
for index, column := range columns {
|
||||||
if field, ok := fields[column]; ok {
|
if field, ok := fields[column]; ok && !foundFields[column] {
|
||||||
if field.Field.Kind() == reflect.Ptr {
|
if field.Field.Kind() == reflect.Ptr {
|
||||||
values[index] = field.Field.Addr().Interface()
|
values[index] = field.Field.Addr().Interface()
|
||||||
} else {
|
} else {
|
||||||
values[index] = reflect.New(reflect.PtrTo(field.Field.Type())).Interface()
|
values[index] = reflect.New(reflect.PtrTo(field.Field.Type())).Interface()
|
||||||
}
|
}
|
||||||
|
foundFields[column] = true
|
||||||
} else {
|
} else {
|
||||||
var i interface{}
|
var i interface{}
|
||||||
values[index] = &i
|
values[index] = &i
|
||||||
|
@ -245,14 +248,16 @@ func (scope *Scope) handleManyToManyPreload(field *Field, conditions []interface
|
||||||
|
|
||||||
var sourceKey []interface{}
|
var sourceKey []interface{}
|
||||||
|
|
||||||
|
var scannedFields = map[string]bool{}
|
||||||
for index, column := range columns {
|
for index, column := range columns {
|
||||||
value := values[index]
|
value := values[index]
|
||||||
if field, ok := fields[column]; ok {
|
if field, ok := fields[column]; ok && !scannedFields[column] {
|
||||||
if field.Field.Kind() == reflect.Ptr {
|
if field.Field.Kind() == reflect.Ptr {
|
||||||
field.Field.Set(reflect.ValueOf(value).Elem())
|
field.Field.Set(reflect.ValueOf(value).Elem())
|
||||||
} else if v := reflect.ValueOf(value).Elem().Elem(); v.IsValid() {
|
} else if v := reflect.ValueOf(value).Elem().Elem(); v.IsValid() {
|
||||||
field.Field.Set(v)
|
field.Field.Set(v)
|
||||||
}
|
}
|
||||||
|
scannedFields[column] = true
|
||||||
} else if strInSlice(column, sourceKeys) {
|
} else if strInSlice(column, sourceKeys) {
|
||||||
sourceKey = append(sourceKey, *(value.(*interface{})))
|
sourceKey = append(sourceKey, *(value.(*interface{})))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue