diff --git a/project_types/wheels/standards/code-style/cfml-style.md b/project_types/wheels/standards/code-style/cfml-style.md index d82a3c39..54f3b0db 100644 --- a/project_types/wheels/standards/code-style/cfml-style.md +++ b/project_types/wheels/standards/code-style/cfml-style.md @@ -197,8 +197,8 @@ component extends="Model" { softDelete(true); // Automatic timestamps - timeStampOnCreate(true); - timeStampOnUpdate(true); + set(timeStampOnCreateProperty="createdAt"); + set(timeStampOnUpdateProperty="updatedAt"); } /** @@ -212,7 +212,7 @@ component extends="Model" { * Check if user has specific role */ function hasRole(required string roleName) { - return this.roles().exists(where="name = ?", whereParams=[arguments.roleName]); + return this.roles().exists(where="name = '#arguments.roleName#'"); } /** @@ -358,8 +358,7 @@ function create() { ```cfml // Finding records users = model("User").findAll( - where="isActive = ? AND createdAt > ?", - whereParams=[true, DateAdd("d", -30, Now())], + where="isActive = true AND createdAt > '#DateAdd("d", -30, Now())#'", order="lastName ASC, firstName ASC", include="profile,posts", page=1, @@ -590,14 +589,13 @@ component extends="Controller" { // Efficient data loading with specific selects users = model("User").findAll( select="id, firstName, lastName, email", - where="isActive = ?", - whereParams=[true], + where="isActive = true", include="profile", // Eager load to avoid N+1 order="lastName ASC" ); // Use exists() instead of count() for boolean checks -hasActiveUsers = model("User").exists(where="isActive = ?", whereParams=[true]); +hasActiveUsers = model("User").exists(where="isActive = true"); // Pagination for large datasets pagedUsers = model("User").findAll( @@ -664,14 +662,12 @@ function create() { ```cfml // Always use parameterized queries users = model("User").findAll( - where="lastName = ? AND isActive = ?", - whereParams=[params.lastName, true] // Parameters are automatically escaped + where="lastName = '#params.lastName#' AND isActive = true" ); // For complex queries users = model("User").findAll( - where="firstName LIKE ? OR lastName LIKE ?", - whereParams=["%#params.search#%", "%#params.search#%"] + where="firstName LIKE '%#params.search#%' OR lastName LIKE '%#params.search#%'" ); ``` @@ -746,8 +742,7 @@ function processData() { // DO: Use parameterized queries users = model("User").findAll( - where="name = ?", - whereParams=[params.name] + where="name = '#params.name#'" ); ``` diff --git a/project_types/wheels/standards/wheels-cli-reference.md b/project_types/wheels/standards/wheels-cli-reference.md index 6ce73add..1193e73f 100644 --- a/project_types/wheels/standards/wheels-cli-reference.md +++ b/project_types/wheels/standards/wheels-cli-reference.md @@ -272,7 +272,7 @@ component extends="wheels.migrator.Migration" { t.string(columnName="firstName"); t.string(columnName="lastName"); t.string(columnName="email"); - t.boolean(columnName="isActive", defaultValue=true); + t.boolean(columnName="isActive", default=true); t.timestamps(); // Adds createdAt, updatedAt t.create(); } @@ -291,8 +291,8 @@ component extends="wheels.migrator.Migration" { component extends="wheels.migrator.Migration" { function up() { transaction { - addIndex(table="users", columns="email", unique=true); - addIndex(table="posts", columns="userId,createdAt"); + addIndex(table="users", columnNames="email", unique=true); + addIndex(table="posts", columnNames="userId,createdAt"); } } diff --git a/project_types/wheels/standards/wheels-model-patterns.md b/project_types/wheels/standards/wheels-model-patterns.md index 46d45338..576a5490 100644 --- a/project_types/wheels/standards/wheels-model-patterns.md +++ b/project_types/wheels/standards/wheels-model-patterns.md @@ -85,7 +85,7 @@ function config() { ### Custom Validations ```cfml function config() { - validates(property="password", method="validatePasswordStrength"); + validate(property="password", method="validatePasswordStrength"); } private void function validatePasswordStrength() { @@ -155,18 +155,30 @@ stats = model("Order").findAll( ); ``` -## Scopes +## Reusable Queries + +### Custom Finder Methods +In Wheels, models don’t support `scope()` like Rails. +Instead, you can define **custom finder methods** that wrap `findAll()` or `findOne()` with your common conditions. -### Named Scopes ```cfml -function config() { - scope(name="active", where="status = 'active'"); - scope(name="recent", where="createdAt > #createDate()#", order="createdAt DESC"); +function findActive() { + return findAll( + where = "status = 'active'" + ); } +function findRecent() { + return findAll( + where = "createdAt > #createDate()#", + order = "createdAt DESC" + ); +} + + // Usage -activeUsers = model("User").active().findAll(); -recentOrders = model("Order").recent().findAll(); +activeUsers = model("User").findActive(); +recentUsers = model("User").findRecent(); ``` ## Data Manipulation