|
| 1 | +# Query Scopes |
| 2 | + |
| 3 | +Query scopes let you define reusable, composable query fragments on your models. Instead of repeating the same `where` or `order` clauses across your application, define them once in the model's `config()` and chain them together. |
| 4 | + |
| 5 | +## Defining Scopes |
| 6 | + |
| 7 | +Add scopes in your model's `config()` function using `scope()`: |
| 8 | + |
| 9 | +```cfm |
| 10 | +component extends="Model" { |
| 11 | + function config() { |
| 12 | + // Static scopes — fixed query fragments |
| 13 | + scope(name="active", where="status = 'active'"); |
| 14 | + scope(name="recent", order="createdAt DESC"); |
| 15 | + scope(name="limited", maxRows=10); |
| 16 | +
|
| 17 | + // Dynamic scope — accepts arguments at call time |
| 18 | + scope(name="byRole", handler="scopeByRole"); |
| 19 | + } |
| 20 | +
|
| 21 | + private struct function scopeByRole(required string role) { |
| 22 | + return {where: "role = '#arguments.role#'"}; |
| 23 | + } |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +### Static Scopes |
| 28 | + |
| 29 | +Static scopes define a fixed set of query parameters. The `scope()` function accepts these keys: |
| 30 | + |
| 31 | +| Parameter | Description | |
| 32 | +|-----------|-------------| |
| 33 | +| `name` | The scope name (becomes the chainable method) | |
| 34 | +| `where` | SQL WHERE clause fragment | |
| 35 | +| `order` | SQL ORDER BY clause | |
| 36 | +| `select` | Columns to select | |
| 37 | +| `include` | Associated models to include | |
| 38 | +| `maxRows` | Limit the number of rows returned | |
| 39 | + |
| 40 | +### Dynamic Scopes |
| 41 | + |
| 42 | +Dynamic scopes use a `handler` function that returns a struct of query parameters. The handler receives whatever arguments the caller passes: |
| 43 | + |
| 44 | +```cfm |
| 45 | +scope(name="olderThan", handler="scopeOlderThan"); |
| 46 | +
|
| 47 | +private struct function scopeOlderThan(required numeric age) { |
| 48 | + return {where: "age > #arguments.age#"}; |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +## Using Scopes |
| 53 | + |
| 54 | +Call scopes as methods on the model, then finish with a terminal method like `findAll()`, `findOne()`, `count()`, or `exists()`: |
| 55 | + |
| 56 | +```cfm |
| 57 | +// Single scope |
| 58 | +users = model("User").active().findAll(); |
| 59 | +
|
| 60 | +// Chained scopes — conditions are AND'd together |
| 61 | +users = model("User").active().recent().findAll(); |
| 62 | +
|
| 63 | +// Dynamic scope with argument |
| 64 | +admins = model("User").byRole("admin").findAll(); |
| 65 | +
|
| 66 | +// With additional finder arguments |
| 67 | +users = model("User").active().findAll(page=1, perPage=25); |
| 68 | +``` |
| 69 | + |
| 70 | +## How Scopes Combine |
| 71 | + |
| 72 | +When you chain multiple scopes: |
| 73 | + |
| 74 | +- **WHERE** clauses are combined with `AND` |
| 75 | +- **ORDER BY** clauses are appended (first scope's order comes first) |
| 76 | +- **INCLUDE** clauses are appended |
| 77 | +- **SELECT** uses the last scope's value (last wins) |
| 78 | +- **maxRows** uses the smallest value |
| 79 | + |
| 80 | +```cfm |
| 81 | +// These two WHERE clauses become: (status = 'active') AND (role = 'admin') |
| 82 | +model("User").active().byRole("admin").findAll(); |
| 83 | +``` |
| 84 | + |
| 85 | +## Terminal Methods |
| 86 | + |
| 87 | +After building a scope chain, call one of these to execute the query: |
| 88 | + |
| 89 | +| Method | Returns | |
| 90 | +|--------|---------| |
| 91 | +| `findAll()` | Query result set | |
| 92 | +| `findOne()` | Single model object | |
| 93 | +| `findByKey(key)` | Model object by primary key | |
| 94 | +| `count()` | Number of matching records | |
| 95 | +| `exists()` | Boolean | |
| 96 | +| `average(property)` | Average value | |
| 97 | +| `sum(property)` | Sum of values | |
| 98 | +| `maximum(property)` | Maximum value | |
| 99 | +| `minimum(property)` | Minimum value | |
| 100 | +| `updateAll(...)` | Updates matching records | |
| 101 | +| `deleteAll()` | Deletes matching records | |
| 102 | +| `findEach(callback)` | Batch iteration (one at a time) | |
| 103 | +| `findInBatches(callback)` | Batch iteration (groups) | |
| 104 | + |
| 105 | +## Transitioning to the Query Builder |
| 106 | + |
| 107 | +You can move from a scope chain into the chainable query builder at any point: |
| 108 | + |
| 109 | +```cfm |
| 110 | +model("User") |
| 111 | + .active() |
| 112 | + .where("age", ">", 18) |
| 113 | + .orderBy("name", "ASC") |
| 114 | + .get(); |
| 115 | +``` |
| 116 | + |
| 117 | +See [Query Builder](query-builder.md) for the full fluent API. |
0 commit comments