Skip to content

Model Object

update()

update() — returns boolean

Available in: model Category: CRUD Functions

Updates an existing model object with the supplied properties and saves the changes to the database. It returns true if the save was successful and false otherwise. You can pass properties directly as named arguments or as a struct. Additional options allow you to control validation, callbacks, transactions, parameterization, cache reloading, and explicit timestamp handling. This method also works seamlessly with associations, making it possible to update related objects in hasOne or hasMany relationships.

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 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.)
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.
allowExplicitTimestampsbooleannofalseSet this to true to allow explicit assignment of updatedAt property
1. Get a post object and then update its title in the database
post = model("post").findByKey(33);
post.update(title="New version of Wheels just released");

2. Get a post object and then update its title and other properties based on what is pased in from the URL/form
post = model("post").findByKey(params.key);
post.update(title="New version of Wheels just released", properties=params.post);

3. If you have a `hasOne` association setup from `author` to `bio`, you can do a scoped call. (The `setBio` method below will call `bio.update(authorId=anAuthor.id)` internally.)
author = model("author").findByKey(params.authorId); 
bio = model("bio").findByKey(params.bioId); 
author.setBio(bio); 

4. If you have a `hasMany` association setup from `owner` to `car`, you can do a scoped call. (The `addCar` method below will call `car.update(ownerId=anOwner.id)` internally.)
anOwner = model("owner").findByKey(params.ownerId); 
aCar = model("car").findByKey(params.carId); 
anOwner.addCar(aCar); 

5. If you have a `hasMany` association setup from `post` to `comment`, you can do a scoped call. (The `removeComment` method below will call `comment.update(postId="")` internally.)
aPost = model("post").findByKey(params.postId); 
aComment = model("comment").findByKey(params.commentId); 
aPost.removeComment(aComment); // Get an object, and toggle a boolean property
user = model("user").findByKey(58); 
isSuccess = user.toggle("isActive"); // returns whether the object was saved properly

// You can also use a dynamic helper for this
isSuccess = user.toggleIsActive();