Refactor handleHasManyPreload

This commit is contained in:
Jinzhu 2016-05-09 22:42:07 +08:00
parent 4786e830d6
commit 0fd2fd66d5
1 changed files with 3 additions and 8 deletions

View File

@ -160,15 +160,10 @@ func (scope *Scope) handleHasManyPreload(field *Field, conditions []interface{})
preloadMap[toString(foreignValues)] = append(preloadMap[toString(foreignValues)], result) preloadMap[toString(foreignValues)] = append(preloadMap[toString(foreignValues)], result)
} }
objects := scope.IndirectValue() for j := 0; j < indirectScopeValue.Len(); j++ {
for j := 0; j < objects.Len(); j++ { object := indirect(indirectScopeValue.Index(j))
object := reflect.Indirect(objects.Index(j))
objectRealValue := getValueFromFields(object, relation.AssociationForeignFieldNames) objectRealValue := getValueFromFields(object, relation.AssociationForeignFieldNames)
objectStringValue := toString(objectRealValue) if results, ok := preloadMap[toString(objectRealValue)]; ok {
if results, ok := preloadMap[objectStringValue]; ok {
if object.Kind() == reflect.Ptr {
object = object.Elem()
}
f := object.FieldByName(field.Name) f := object.FieldByName(field.Name)
f.Set(reflect.Append(f, results...)) f.Set(reflect.Append(f, results...))
} }