Return to v2.5.0 docs

update()


Model Object CRUD Functions boolean model


Updates the object with the supplied properties and saves it to the database. Returns true if the object was saved successfully to the database and false otherwise.

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

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

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

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

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

Related Functions

CRUD Functions