Model Class
create()
Signature
Section titled “Signature”create() — returns any
Available in: model
Category: Create Functions
Description
Section titled “Description”The create() method is used to instantiate a new model object, set its properties, and save it to the database (if validations pass). Even if validation fails, the method still returns the unsaved object, including any validation errors. It’s a higher-level convenience function that combines object creation, property assignment, validation, and saving into a single call. Property names and values can be passed in either using named arguments or as a struct to the properties argument.
Parameters
Section titled “Parameters”| Name | Type | Required | Default | Description |
|---|---|---|---|---|
properties | struct | no | [runtime expression] | The properties you want to set on the object (can also be passed in as named arguments). |
parameterize | any | no | true | Set to true to use cfqueryparam on all columns, or pass in a list of property names to use cfqueryparam on those only. |
reload | boolean | no | false | Set to true to force Wheels to query the database even though an identical query for this model may have been run in the same request. (The default in Wheels is to get the second query from the model’s request-level cache.) |
validate | boolean | no | true | Set to false to skip validations for this operation. |
transaction | string | no | [runtime expression] | Set this to commit to update the database, rollback to run all the database queries but not commit them, or none to skip transaction handling altogether. |
callbacks | boolean | no | true | Set to false to disable callbacks for this method. |
allowExplicitTimestamps | boolean | no | false | Set this to true to allow explicit assignment of createdAt or updatedAt properties |
Examples
Section titled “Examples”1. Create a new author and save it to the database
newAuthor = model("author").create(params.author);
2. Same as above using named arguments
newAuthor = model("author").create(firstName="John", lastName="Doe");
3. Same as above using both named arguments and a struct
newAuthor = model("author").create(active=1, properties=params.author);
4. If you have a `hasOne` or `hasMany` association setup from `customer` to `order`, you can do a scoped call. (The `createOrder` method below will call `model("order").create(customerId=aCustomer.id, shipping=params.shipping)` internally.)
aCustomer = model("customer").findByKey(params.customerId);
anOrder = aCustomer.createOrder(shipping=params.shipping);