Skip to content

Model Class

create()

create() — returns any

Available in: model Category: Create Functions

Creates a new object, saves it to the database (if the validation permits it), and returns it. If the validation fails, the unsaved object (with errors added to it) is still returned. Property names and values can be passed in either using named arguments or as a struct to the properties argument.

NameTypeRequiredDefaultDescription
propertiesstructno[runtime expression]The properties you want to set on the object (can also be passed in as named arguments).
parameterizeanynotrueSet to true to use cfqueryparam on all columns, or pass in a list of property names to use cfqueryparam on those only.
reloadbooleannofalseSet to true to force CFWheels to query the database even though an identical query for this model may have been run in the same request. (The default in CFWheels is to get the second query from the model’s request-level cache.)
validatebooleannotrueSet to false to skip validations for this operation.
transactionstringno[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.
callbacksbooleannotrueSet to false to disable callbacks for this method.
// Create a new author and save it to the database
newAuthor = model("author").create(params.author);

// Same as above using named arguments
newAuthor = model("author").create(firstName="John", lastName="Doe");

// Same as above using both named arguments and a struct
newAuthor = model("author").create(active=1, properties=params.author);

// 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);