Support only preloading (#1926)

* add support for only preloading relations on an already populated model

* Update callback_query.go

comments
This commit is contained in:
gstvg 2018-09-09 19:52:32 -03:00 committed by Jinzhu
parent 588b598f9f
commit 282f11af19
2 changed files with 10 additions and 0 deletions

View File

@ -19,6 +19,11 @@ func queryCallback(scope *Scope) {
return return
} }
//we are only preloading relations, dont touch base model
if _, skip := scope.InstanceGet("gorm:only_preload"); skip {
return
}
defer scope.trace(NowFunc()) defer scope.trace(NowFunc())
var ( var (

View File

@ -314,6 +314,11 @@ func (s *DB) Find(out interface{}, where ...interface{}) *DB {
return s.NewScope(out).inlineCondition(where...).callCallbacks(s.parent.callbacks.queries).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 // Scan scan value to a struct
func (s *DB) Scan(dest interface{}) *DB { func (s *DB) Scan(dest interface{}) *DB {
return s.NewScope(s.Value).Set("gorm:query_destination", dest).callCallbacks(s.parent.callbacks.queries).db return s.NewScope(s.Value).Set("gorm:query_destination", dest).callCallbacks(s.parent.callbacks.queries).db