Fix can't load preload fields correctly

This commit is contained in:
Jinzhu 2015-09-26 00:41:07 +08:00
parent 198fc47051
commit 37bf87aa29
2 changed files with 8 additions and 3 deletions

View File

@ -56,11 +56,11 @@ func (field *Field) Set(value interface{}) error {
func (scope *Scope) Fields() map[string]*Field { func (scope *Scope) Fields() map[string]*Field {
if scope.fields == nil { if scope.fields == nil {
fields := map[string]*Field{} fields := map[string]*Field{}
structFields := scope.GetStructFields() modelStruct := scope.GetModelStruct()
indirectValue := scope.IndirectValue() indirectValue := scope.IndirectValue()
isStruct := indirectValue.Kind() == reflect.Struct isStruct := indirectValue.Kind() == reflect.Struct
for _, structField := range structFields { for _, structField := range modelStruct.StructFields {
if isStruct { if isStruct {
fields[structField.DBName] = getField(indirectValue, structField) fields[structField.DBName] = getField(indirectValue, structField)
} else { } else {
@ -68,7 +68,10 @@ func (scope *Scope) Fields() map[string]*Field {
} }
} }
scope.fields = fields if modelStruct.cached {
scope.fields = fields
}
return fields
} }
return scope.fields return scope.fields
} }

View File

@ -23,6 +23,7 @@ type ModelStruct struct {
StructFields []*StructField StructFields []*StructField
ModelType reflect.Type ModelType reflect.Type
defaultTableName string defaultTableName string
cached bool
} }
func (s ModelStruct) TableName(db *DB) string { func (s ModelStruct) TableName(db *DB) string {
@ -372,6 +373,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
modelStructs[scopeType] = &modelStruct modelStructs[scopeType] = &modelStruct
<-finished <-finished
modelStruct.cached = true
return &modelStruct return &modelStruct
} }