mirror of https://github.com/go-gorm/gorm.git
Fix get type Name from scope
This commit is contained in:
parent
f8c3ad6af3
commit
4677215ef4
|
@ -88,6 +88,12 @@ func TestRelated(t *testing.T) {
|
||||||
t.Errorf("Should have two emails")
|
t.Errorf("Should have two emails")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var emails3 []*Email
|
||||||
|
DB.Model(&user).Related(&emails3)
|
||||||
|
if len(emails3) != 2 {
|
||||||
|
t.Errorf("Should have two emails")
|
||||||
|
}
|
||||||
|
|
||||||
var user1 User
|
var user1 User
|
||||||
DB.Model(&user).Related(&user1.Emails)
|
DB.Model(&user).Related(&user1.Emails)
|
||||||
if len(user1.Emails) != 2 {
|
if len(user1.Emails) != 2 {
|
||||||
|
|
|
@ -407,18 +407,20 @@ func (scope *Scope) count(value interface{}) *Scope {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) typeName() string {
|
func (scope *Scope) typeName() string {
|
||||||
value := scope.IndirectValue()
|
typ := scope.IndirectValue().Type()
|
||||||
if value.Kind() == reflect.Slice {
|
|
||||||
return value.Type().Elem().Name()
|
for typ.Kind() == reflect.Slice || typ.Kind() == reflect.Ptr {
|
||||||
|
typ = typ.Elem()
|
||||||
}
|
}
|
||||||
|
|
||||||
return value.Type().Name()
|
return typ.Name()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
|
func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
|
||||||
toScope := scope.db.NewScope(value)
|
toScope := scope.db.NewScope(value)
|
||||||
fromFields := scope.Fields()
|
fromFields := scope.Fields()
|
||||||
toFields := toScope.Fields()
|
toFields := toScope.Fields()
|
||||||
|
|
||||||
for _, foreignKey := range append(foreignKeys, toScope.typeName()+"Id", scope.typeName()+"Id") {
|
for _, foreignKey := range append(foreignKeys, toScope.typeName()+"Id", scope.typeName()+"Id") {
|
||||||
var fromField, toField *Field
|
var fromField, toField *Field
|
||||||
if field, ok := scope.FieldByName(foreignKey); ok {
|
if field, ok := scope.FieldByName(foreignKey); ok {
|
||||||
|
|
Loading…
Reference in New Issue