Skip to content

Commit 60330ef

Browse files
github-actions[bot]wheels-docs-validator[bot]
andauthored
docs(api): validate Model Configuration section (run 25476347078) (wheels-dev#2461)
Co-authored-by: wheels-docs-validator[bot] <wheels-docs-validator@users.noreply.github.com>
1 parent b846ca2 commit 60330ef

46 files changed

Lines changed: 1645 additions & 117 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tools/docs-validation/state.json

Lines changed: 839 additions & 0 deletions
Large diffs are not rendered by default.

vendor/wheels/model/associations.cfc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ component {
1111
* @foreignKey Foreign key property name (usually not needed if you follow Wheels conventions since the foreign key name will be deduced from the `name` argument).
1212
* @joinKey Column name to join to if not the primary key (usually not needed if you follow Wheels conventions since the join key will be the table's primary key/keys).
1313
* @joinType Use to set the join type when joining associated tables. Possible values are `inner` (for `INNER JOIN`) and `outer` (for `LEFT OUTER JOIN`).
14+
* @polymorphic Set to `true` to declare a polymorphic `belongsTo` association. The foreign key defaults to `{name}Id` and a `{name}Type` column is used to store the owning model name at runtime.
1415
*/
1516
public void function belongsTo(
1617
required string name,
@@ -56,6 +57,7 @@ component {
5657
* @dependent Defines how to handle dependent model objects when you delete an object from this model. `delete` / `deleteAll` deletes the record(s) (`deleteAll` bypasses object instantiation). `remove` / `removeAll` sets the forein key field(s) to `NULL` (`removeAll` bypasses object instantiation).
5758
* @shortcut Set this argument to create an additional dynamic method that gets the object(s) from the other side of a many-to-many association.
5859
* @through Set this argument if you need to override Wheels conventions when using the `shortcut` argument. Accepts a list of two association names representing the chain from the opposite side of the many-to-many relationship to this model.
60+
* @as Set this argument to declare a polymorphic `hasMany` association. The child model stores the parent type in a `{as}Type` column alongside the foreign key `{as}Id`.
5961
*/
6062
public void function hasMany(
6163
required string name,
@@ -111,6 +113,7 @@ component {
111113
* @joinKey [see:belongsTo].
112114
* @joinType [see:belongsTo].
113115
* @dependent [see:hasMany].
116+
* @as Set this argument to declare a polymorphic `hasOne` association. The child model stores the parent type in a `{as}Type` column alongside the foreign key `{as}Id`.
114117
*/
115118
public void function hasOne(
116119
required string name,
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
// Make `isActive` the only property that can be set through mass assignment operations like `updateAll()`.
1+
// 1. Allow only `isActive` to be set through mass assignment (e.g. `updateAll()`, `new()`, `update()`).
22
config() {
33
accessibleProperties("isActive");
44
}
5+
6+
// 2. Allow a comma-delimited list of properties to be set through mass assignment.
7+
// Any property not in this list is silently ignored when set via mass assignment.
8+
config() {
9+
accessibleProperties("firstName,lastName,email");
10+
}
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,16 @@
1-
// Instruct CFWheels to call the `fixObj` method after an object has been created.
2-
afterCreate("fixObj");
1+
// 1. Register a single method to run after an object is created
2+
// In models/User.cfc
3+
component extends="Model" {
4+
function config() {
5+
afterCreate("sendWelcomeEmail");
6+
}
7+
private function sendWelcomeEmail() {
8+
// send email to this.email
9+
}
10+
}
11+
12+
// 2. Register multiple methods by passing a comma-delimited list
13+
afterCreate("updateCache,notifyAdmin");
14+
15+
// 3. Register using the named `methods` argument
16+
afterCreate(methods="syncToExternalApi");
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
1-
// Instruct CFWheels to call the `fixObj` method after an object has been deleted.
2-
afterDelete("fixObj");
1+
// 1. Call a single method after an object is deleted
2+
// In models/Order.cfc
3+
afterDelete("notifyWarehouse");
4+
5+
// 2. Call multiple methods after an object is deleted (comma-separated list)
6+
// In models/User.cfc
7+
afterDelete("removeFromSearchIndex,clearCachedData");
8+
9+
// 3. Register several after-delete callbacks individually for clarity
10+
// In models/Article.cfc
11+
afterDelete("logDeletion");
12+
afterDelete("cleanupAttachments");
Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,29 @@
1-
// Instruct CFWheels to call the `setTime` method after getting objects or records with one of the finder methods.
1+
// 1. Register a single callback method to run after records are fetched
2+
// In models/User.cfc
23
config() {
3-
afterFind("setTime");
4+
afterFind("setFetchedAt");
45
}
56

6-
function setTime(){
7+
// The callback receives each row's columns as arguments; return the struct to modify the record.
8+
function setFetchedAt() {
79
arguments.fetchedAt = Now();
810
return arguments;
911
}
12+
13+
// 2. Format a column value after a find (works for both query rows and objects)
14+
// In models/Product.cfc
15+
config() {
16+
afterFind("formatPrice");
17+
}
18+
19+
function formatPrice() {
20+
if (StructKeyExists(arguments, "price")) {
21+
arguments.price = DollarFormat(arguments.price);
22+
}
23+
return arguments;
24+
}
25+
26+
// 3. Register multiple callback methods as a comma-delimited list
27+
config() {
28+
afterFind("setFetchedAt,formatPrice");
29+
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
// Instruct CFWheels to call the `fixObj` method after an object has been initialized (i.e. after creating it or fetching it with a finder method).
1+
// 1. Call a single method after any object is initialized (whether new or fetched from the database)
22
afterInitialization("fixObj");
3+
4+
// 2. Call multiple methods after initialization by passing a comma-delimited list
5+
afterInitialization("setDefaults,fixObj");
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,17 @@
1-
// Instruct CFWheels to call the `fixObj` method after a new object has been created.
2-
afterNew("fixObj");
1+
// 1. Call a single method after a new object is initialized
2+
// In models/User.cfc
3+
component extends="Model" {
4+
function config() {
5+
afterNew("setDefaults");
6+
}
7+
private function setDefaults() {
8+
this.role = "member";
9+
this.active = true;
10+
}
11+
}
12+
13+
// 2. Call multiple methods after a new object is initialized (comma-delimited list)
14+
afterNew("setDefaults,generateToken");
15+
16+
// 3. Use the `method` argument alias instead of `methods`
17+
afterNew(method="setDefaults");
Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,26 @@
1-
// Instruct CFWheels to call the `fixObj` method after an object has been saved.
2-
afterSave("fixObj");
1+
// 1. Register a single callback method to run after an object is saved (both create and update)
2+
// In models/User.cfc
3+
component extends="Model" {
4+
function config() {
5+
afterSave("sendWelcomeEmail");
6+
}
7+
8+
private function sendWelcomeEmail() {
9+
// called automatically each time a User is saved
10+
}
11+
}
12+
13+
// 2. Register multiple callback methods using a comma-delimited list
14+
component extends="Model" {
15+
function config() {
16+
afterSave("updateSearchIndex,notifyAdmins");
17+
}
18+
}
19+
20+
// 3. Register multiple callbacks by calling afterSave() more than once
21+
component extends="Model" {
22+
function config() {
23+
afterSave("updateSearchIndex");
24+
afterSave("notifyAdmins");
25+
}
26+
}
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,16 @@
1-
// Instruct CFWheels to call the `fixObj` method after an object has been updated.
2-
afterUpdate("fixObj");
1+
// 1. Register a single method to run after an object is updated
2+
// In models/User.cfc
3+
component extends="Model" {
4+
function config() {
5+
afterUpdate("clearCache");
6+
}
7+
private function clearCache() {
8+
// invalidate cached data for this user
9+
}
10+
}
11+
12+
// 2. Register multiple methods by passing a comma-delimited list
13+
afterUpdate("clearCache,notifyAuditLog");
14+
15+
// 3. Register using the named `methods` argument
16+
afterUpdate(methods="syncToSearchIndex");

0 commit comments

Comments
 (0)