forked from mirror/gorm
support to preload all children in multiple levels associations
This commit is contained in:
parent
912360097a
commit
c575a4e719
|
@ -185,12 +185,26 @@ func Preload(db *gorm.DB) {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
preloadFields := strings.Split(name, ".")
|
preloadFields := strings.Split(name, ".")
|
||||||
if _, ok := preloadMap[preloadFields[0]]; !ok {
|
if preloadFields[0] == clause.Associations {
|
||||||
preloadMap[preloadFields[0]] = map[string][]interface{}{}
|
for _, rel := range db.Statement.Schema.Relationships.Relations {
|
||||||
}
|
if rel.Schema == db.Statement.Schema {
|
||||||
|
if _, ok := preloadMap[rel.Name]; !ok {
|
||||||
|
preloadMap[rel.Name] = map[string][]interface{}{}
|
||||||
|
}
|
||||||
|
|
||||||
if value := strings.TrimPrefix(strings.TrimPrefix(name, preloadFields[0]), "."); value != "" {
|
if value := strings.TrimPrefix(strings.TrimPrefix(name, preloadFields[0]), "."); value != "" {
|
||||||
preloadMap[preloadFields[0]][value] = db.Statement.Preloads[name]
|
preloadMap[rel.Name][value] = db.Statement.Preloads[name]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if _, ok := preloadMap[preloadFields[0]]; !ok {
|
||||||
|
preloadMap[preloadFields[0]] = map[string][]interface{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
if value := strings.TrimPrefix(strings.TrimPrefix(name, preloadFields[0]), "."); value != "" {
|
||||||
|
preloadMap[preloadFields[0]][value] = db.Statement.Preloads[name]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue