Lazy call registered scopes

This commit is contained in:
Jinzhu 2021-02-25 20:06:26 +08:00
parent 828e6b646b
commit 6b7d18656d
1 changed files with 4 additions and 5 deletions

View File

@ -240,11 +240,10 @@ func (db *DB) Offset(offset int) (tx *DB) {
// } // }
// //
// db.Scopes(AmountGreaterThan1000, OrderStatus([]string{"paid", "shipped"})).Find(&orders) // db.Scopes(AmountGreaterThan1000, OrderStatus([]string{"paid", "shipped"})).Find(&orders)
func (db *DB) Scopes(funcs ...func(*DB) *DB) *DB { func (db *DB) Scopes(funcs ...func(*DB) *DB) (tx *DB) {
for _, f := range funcs { tx = db.getInstance()
db = f(db) tx.Statement.scopes = append(tx.Statement.scopes, funcs...)
} return tx
return db
} }
// Preload preload associations with given conditions // Preload preload associations with given conditions