add support for only preloading relations on an already populated model

This commit is contained in:
Gustavo de Bona Garcia 2018-05-31 17:23:44 -03:00
parent 82eb9f8a5b
commit 56714685fa
2 changed files with 9 additions and 0 deletions

View File

@ -19,6 +19,10 @@ func queryCallback(scope *Scope) {
return
}
if _, skip := scope.InstanceGet("gorm:only_preload"); skip {
return
}
defer scope.trace(NowFunc())
var (

View File

@ -311,6 +311,11 @@ func (s *DB) Find(out interface{}, where ...interface{}) *DB {
return s.NewScope(out).inlineCondition(where...).callCallbacks(s.parent.callbacks.queries).db
}
//Preloads preloads relations, don`t touch out
func (s *DB) Preloads(out interface{}) *DB {
return s.NewScope(out).InstanceSet("gorm:only_preload", 1).callCallbacks(s.parent.callbacks.queries).db
}
// Scan scan value to a struct
func (s *DB) Scan(dest interface{}) *DB {
return s.NewScope(s.Value).Set("gorm:query_destination", dest).callCallbacks(s.parent.callbacks.queries).db