Model Class
save()
Signature
Section titled “Signature”save() — returns boolean
Available in: model
Category: CRUD Functions
Description
Section titled “Description”Saves the current model object to the database, with Wheels automatically determining whether to perform an INSERT for new objects or an UPDATE for existing ones. It returns true if the object was successfully saved, and false if the object failed validation or could not be saved. By default, save() also respects callbacks, validations, and parameterization, though these behaviors can be customized through optional arguments.
Parameters
Section titled “Parameters”| Name | Type | Required | Default | Description |
|---|---|---|---|---|
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. |
Examples
Section titled “Examples”1. Basic Save (Automatic INSERT/UPDATE)
<cfscript>
user = model("user").new();
user.firstName = "Alice";
user.lastName = "Smith";
user.email = "alice@example.com";
if(user.save()){
writeOutput("User saved successfully!");
} else {
writeOutput("Error saving user. Please check validations.");
}
</cfscript>
2. Save Without Validations
<cfscript>
user = model("user").findByKey(1);
user.firstName = ""; // Normally fails validation
// Save without running validations
user.save(validate=false);
</cfscript>
3. Save Using Specific cfqueryparam Columns
<cfscript>
user = model("user").new();
user.firstName = "Bob";
user.lastName = "Jones";
user.email = "bob@example.com";
// Only parameterize the `email` field
user.save(parameterize="email");
</cfscript>
4. Save Within a Transaction
<cfscript>
user = model("user").new();
user.firstName = "Charlie";
user.lastName = "Brown";
user.email = "charlie@example.com";
// Attempt to save, but roll back instead of committing
user.save(transaction="rollback");
</cfscript>
5. Save and Handle Callbacks Manually
<cfscript>
user = model("user").new();
user.firstName = "Dana";
user.lastName = "White";
user.email = "dana@example.com";
// Save without triggering beforeSave/afterSave callbacks
user.save(callbacks=false);
</cfscript>