Generate a model with properties, validations, and associations.
Synopsis
wheels generate model [name] [options]
wheels g model [name] [options]
Description
The wheels generate model command creates a new model CFC file with optional properties, associations, and database migrations. Models represent database tables and contain business logic, validations, and relationships.
Arguments
Argument
Description
Default
name
Model name (singular)
Required
Options
Option
Description
Default
--migration
Generate migration file
true
--properties
Properties list (name:type)
--belongs-to
Belongs to associations
--has-many
Has many associations
--has-one
Has one associations
--force
Overwrite existing files
false
--help
Show help information
Examples
Basic model
wheels generate model user
Creates:
/models/User.cfc
Migration file (if enabled)
Model with properties
wheels generate model user --properties="firstName:string,lastName:string,email:string,age:integer"
Model with associations
wheels generate model post --belongs-to="user" --has-many="comments"
Model without migration
wheels generate model setting --migration=false
Complex model
wheels generate model product \
--properties="name:string,price:decimal,stock:integer,active:boolean" \
--belongs-to="category,brand" \
--has-many="reviews,orderItems"
Property Types
Type
Database Type
CFML Type
string
VARCHAR(255)
string
text
TEXT
string
integer
INTEGER
numeric
biginteger
BIGINT
numeric
float
FLOAT
numeric
decimal
DECIMAL(10,2)
numeric
boolean
BOOLEAN
boolean
date
DATE
date
datetime
DATETIME
date
timestamp
TIMESTAMP
date
binary
BLOB
binary
uuid
VARCHAR(35)
string
Generated Code
Basic Model
component extends="Model" {
function init() {
// Table name (optional if following conventions)
table("users");
// Validations
validatesPresenceOf("email");
validatesUniquenessOf("email");
validatesFormatOf("email", regex="^[^@]+@[^@]+\.[^@]+$");
// Callbacks
beforeCreate("setDefaultValues");
}
private function setDefaultValues() {
if (!StructKeyExists(this, "createdAt")) {
this.createdAt = Now();
}
}
}