forked from mirror/gorm
Fix scope.getField
This commit is contained in:
parent
139b9a37e7
commit
803343fbe5
8
field.go
8
field.go
|
@ -52,6 +52,12 @@ func (scope *Scope) Fields() map[string]*Field {
|
||||||
|
|
||||||
func (scope *Scope) getField(structField *StructField) *Field {
|
func (scope *Scope) getField(structField *StructField) *Field {
|
||||||
field := Field{StructField: structField}
|
field := Field{StructField: structField}
|
||||||
field.Field = scope.IndirectValue().FieldByName(structField.Name)
|
indirectValue := scope.IndirectValue()
|
||||||
|
if len(structField.Names) > 0 && indirectValue.Kind() == reflect.Struct {
|
||||||
|
for _, name := range structField.Names {
|
||||||
|
indirectValue = reflect.Indirect(indirectValue.FieldByName(name))
|
||||||
|
}
|
||||||
|
field.Field = indirectValue
|
||||||
|
}
|
||||||
return &field
|
return &field
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,8 +18,9 @@ type ModelStruct struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type StructField struct {
|
type StructField struct {
|
||||||
Name string
|
|
||||||
DBName string
|
DBName string
|
||||||
|
Name string
|
||||||
|
Names []string
|
||||||
IsPrimaryKey bool
|
IsPrimaryKey bool
|
||||||
IsScanner bool
|
IsScanner bool
|
||||||
IsTime bool
|
IsTime bool
|
||||||
|
@ -96,7 +97,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
|
|
||||||
reflectValue := reflect.Indirect(reflect.ValueOf(scope.Value))
|
reflectValue := reflect.Indirect(reflect.ValueOf(scope.Value))
|
||||||
if reflectValue.Kind() == reflect.Slice {
|
if reflectValue.Kind() == reflect.Slice {
|
||||||
reflectValue = reflect.Indirect(reflect.New(reflectValue.Elem().Type()))
|
reflectValue = reflect.Indirect(reflect.New(reflectValue.Type().Elem()))
|
||||||
}
|
}
|
||||||
scopeTyp := reflectValue.Type()
|
scopeTyp := reflectValue.Type()
|
||||||
|
|
||||||
|
@ -125,7 +126,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
field := &StructField{Struct: fieldStruct}
|
field := &StructField{Struct: fieldStruct, Name: fieldStruct.Name, Names: []string{fieldStruct.Name}}
|
||||||
if fieldStruct.Tag.Get("sql") == "-" {
|
if fieldStruct.Tag.Get("sql") == "-" {
|
||||||
field.IsIgnored = true
|
field.IsIgnored = true
|
||||||
} else {
|
} else {
|
||||||
|
@ -206,6 +207,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
if _, ok := field.GormSettings["EMBEDDED"]; ok || fieldStruct.Anonymous {
|
if _, ok := field.GormSettings["EMBEDDED"]; ok || fieldStruct.Anonymous {
|
||||||
for _, field := range scope.New(reflect.New(indirectType).Interface()).GetStructFields() {
|
for _, field := range scope.New(reflect.New(indirectType).Interface()).GetStructFields() {
|
||||||
|
field.Names = append([]string{fieldStruct.Name}, field.Names...)
|
||||||
modelStruct.StructFields = append(modelStruct.StructFields, field)
|
modelStruct.StructFields = append(modelStruct.StructFields, field)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
|
|
5
scope.go
5
scope.go
|
@ -95,7 +95,10 @@ func (scope *Scope) HasError() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) PrimaryKeyField() *Field {
|
func (scope *Scope) PrimaryKeyField() *Field {
|
||||||
return scope.getField(scope.GetModelStruct().PrimaryKeyField)
|
if field := scope.GetModelStruct().PrimaryKeyField; field != nil {
|
||||||
|
return scope.getField(field)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrimaryKey get the primary key's column name
|
// PrimaryKey get the primary key's column name
|
||||||
|
|
Loading…
Reference in New Issue