CLI Overview
Quick Start Guide
wheels info
wheels reload
wheels deps
wheels destroy
wheels watch
wheels generate app
wheels generate app-wizard
wheels generate controller
wheels generate model
wheels generate view
wheels generate property
wheels generate route
wheels generate resource
wheels generate api-resource
wheels generate frontend
wheels generate test
wheels generate snippets
wheels scaffold
wheels db create
wheels db drop
wheels db setup
wheels db reset
wheels db status
wheels db version
wheels db rollback
wheels db seed
wheels db dump
wheels db restore
wheels db shell
wheels db schema
wheels dbmigrate info
wheels dbmigrate latest
wheels dbmigrate up
wheels dbmigrate down
wheels dbmigrate reset
wheels dbmigrate exec
wheels dbmigrate create blank
wheels dbmigrate create table
wheels dbmigrate create column
wheels dbmigrate remove table
wheels test
wheels test run
wheels test coverage
wheels test debug
wheels config list
wheels config set
wheels config env
wheels env
wheels env setup
wheels env list
wheels env switch
wheels environment
wheels console
wheels runner
wheels server
wheels server start
wheels server stop
wheels server restart
wheels server status
wheels server log
wheels server open
wheels plugins
wheels plugins list
wheels plugins install
wheels plugins remove
wheels analyze
wheels analyze code
wheels analyze performance
wheels analyze security
wheels security
wheels security scan
wheels optimize
wheels optimize performance
wheels docs
wheels docs generate
wheels docs serve
wheels ci init
wheels docker init
wheels docker deploy
wheels deploy
wheels deploy audit
wheels deploy exec
wheels deploy hooks
wheels deploy init
wheels deploy lock
wheels deploy logs
wheels deploy proxy
wheels deploy push
wheels deploy rollback
wheels deploy secrets
wheels deploy setup
wheels deploy status
wheels deploy stop
Configuration Management
Creating Commands
Service Architecture
Migrations Guide
Testing Guide
Object Relational Mapping
Creating Records
Reading Records
Updating Records
Deleting Records
Column Statistics
Dynamic Finders
Getting Paginated Data
Associations
Nested Properties
Object Validation
Object Callbacks
Calculated Properties
Transactions
Dirty Records
Soft Delete
Automatic Time Stamps
Using Multiple Data Sources
description: Updating records in your database tables.
Updating Records
When you have created or retrieved an object, you can save it to the database by calling its save() method. This method returns true
if the object passes all validations and the object was saved to the database. Otherwise, it returns false
.
This chapter will focus on how to update records. Read the Creating Records chapter for more information about how to create new records.
A Practical Example
Let's start with an example of getting a blog post from the database, updating its title, and saving it back:
post = model("post").findByKey(33);
post.title = "New version of Wheels just released";
post.save();
You can also change the values of one or more properties and save them to the database in one single call using the update()
method, like this:
post = model("post").findByKey(33);
post.update(title="New version of Wheels just released");
Updating Via struct Values
You can also pass in name/value pairs to update()
as a struct. The main reason this method accepts a struct is to allow you to easily use it with forms.
This is how it would look if you wanted to update the properties for a post based on a submitted form.
post = model("post").findByKey(params.key);
post.update(params.post);
It's also possible to combine named arguments with a struct, but then you need to name the struct argument as properties
.
Example:
post = model("post").findByKey(params.key);
post.update(title="New version of Wheels just released", properties=params.post);
Combine Reading and Updating into a Single Call
To cut down even more on lines of code, you can also combine the reading and saving of the objects by using the class-level methods updateByKey() and updateAll().
The updateByKey() Method
Give the updateByKey() method a primary key value (or several if you use composite keys) in the key
argument, and it will update the corresponding record in your table with the properties you give it. You can pass in the properties either as named arguments or as a struct to the properties
argument.
This method returns the object with the primary key value you specified. If the object does not pass validation, it will be returned anyway, but nothing will be saved to the database.
By default, updateByKey() will fetch the object first and call the update()
method on it, thus invoking any callbacks and validations you have specified for the model. You can change this behavior by passing in instantiate=false
. Then it will just update the record from the table using a simple UPDATE query.
An example of using updateByKey() by passing a struct:
result = model("post").updateByKey(33, params.post);
And an example of using updateByKey() by passing named arguments:
result = model("post").updateByKey(id=33, title="New version of Wheels just released", published=1);
Updating Multiple Rows with updateAll()
The updateAll() method allows you to update more than one record in a single call. You specify what records to update with the where
argument and tell Wheels what updates to make using named arguments for the properties.
The where
argument is used exactly as you specify it in the WHERE
clause of the query (with the exception that Wheels automatically wraps everything properly in cfqueryparam
tags). So make sure that you place those commas and quotes correctly!
An example:
recordsReturned = model("post").updateAll(
published=1, publishedAt=Now(), where="published=0"
);
Unlike updateByKey(), the updateAll() method will not instantiate the objects by default. That could be really slow if you wanted to update a lot of records at once.