Skip to content

Commit

Permalink
Fix can't load preload fields correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Sep 25, 2015
1 parent 198fc47 commit 37bf87a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 6 additions & 3 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,22 @@ func (field *Field) Set(value interface{}) error {
func (scope *Scope) Fields() map[string]*Field {
if scope.fields == nil {
fields := map[string]*Field{}
structFields := scope.GetStructFields()
modelStruct := scope.GetModelStruct()

indirectValue := scope.IndirectValue()
isStruct := indirectValue.Kind() == reflect.Struct
for _, structField := range structFields {
for _, structField := range modelStruct.StructFields {
if isStruct {
fields[structField.DBName] = getField(indirectValue, structField)
} else {
fields[structField.DBName] = &Field{StructField: structField, IsBlank: true}
}
}

scope.fields = fields
if modelStruct.cached {
scope.fields = fields
}
return fields
}
return scope.fields
}
Expand Down
2 changes: 2 additions & 0 deletions model_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type ModelStruct struct {
StructFields []*StructField
ModelType reflect.Type
defaultTableName string
cached bool
}

func (s ModelStruct) TableName(db *DB) string {
Expand Down Expand Up @@ -372,6 +373,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
modelStructs[scopeType] = &modelStruct

<-finished
modelStruct.cached = true

return &modelStruct
}
Expand Down

0 comments on commit 37bf87a

Please sign in to comment.