Untitled Document
Understanding and Using CFCs (with OOP and database interaction)
Part 6
Editing / Updating our job data
Now we have methods that will search, add, and display our job information from the database.
Next we need a method that will update or edit out data.
Remember that our results array that is returned from searching the database is an array of objects, or components. The returned results are not simple structures with data in them. This means that every method we add into our jobs.cfc file is available to each of these results objects.
This is an imporant distinction because the update method we will be creating will be executed on the particular job component object we want updated.
For example, if we wanted to update the result items one, two and three, our code to do so would be :
results[1].name = 'test 1';
results[1].update();
results[2].name = 'test 2';
results[2].update();
results[3].name = 'test 3';
results[3].update();
Because each of these results array items are components, they automatically contain all data fields and information that we will need to make this update to the database! The only thing that wee need to specify is the datafields that are actually going to change, all other data is already there.
Creating our edit page
The first step is to create the form to edit our data. Again, formatting has intentionally been limited to focus on the ColdFusion code.
We've added some simple scripting to retreive the data for the record we want to edit. This code is very similar to the code that we did in part five.
What we've done differently is that we've specified a search criteria, in this case, the id of the job record we want to get. The id of the record is being sent to this page from the listings page we created in part five.
We will call this page job_edit.cfm.
<html><head><title>Edit Job Form</title></head>
<body>
Edit job<br>
<!--- id of the job record to edit, being sent from jobs.cfm --->
<cfparam name="url.id" default="0" type="numeric">
<cfscript>
// create a job component to do our searching
search = createObject("component", "job");
// specify search criteria, in this case, the id of the record
search.id = url.id;
// perform the search and return an array of results
result = search.searchJobs();
</cfscript>
<cfoutput>
<!--- submit to the job_edit_action.cfm page, and again, pass the id --->
<form action="job_edit_action.cfm?id=#url.id#" method="post">
<!--- populate the name field from our results from our search above --->
job name : <input type="text" name="name" value="#result[1].name#">
<br><br>
<input type="submit" value="edit job">
</form>
</cfoutput>
</body>
</html>
Creating our update method
Now we need to create a method that updates our database record with information contained in the current (keyword: this) component.
Because we are updating based on the current component that is executing the method, all of the data we will need is in the "this" scope.
This is possibly the most confusing portion of our component so far, but it is also the most powerful. To demonstrate what is going to happen, lets look at what we will be doing step by step.
1) Perform a search, an array of components is returned, thus for example results[1] is actually a copy of the job component.
2) The results[1] job component that was created, has been filled with all of the information for the particular record. This is done by using the "this" scope, so for example, if the record id was 1400, then our search method sets "this.id" equal to "1400", and outside the method, results[1].id likewise will equal "1400"
3) Change any of the information that has changed. We only have to set the information we are changing, because all the other data properties have been filled with our search method for us!
4) Execute our update method of the particular component we want updated. This means we are executing results[1].updateJob().
5) Our update method will update the information with data contained in the "this" scope.
Now lets look at the method we'll add to our job.cfc file to handle the update method.
<cffunction name="updateJob">
<cfquery datasource="#request.dsn#" name="qUpdate">
update jobs
set display_name = '#this.name#'
where
job_id = #this.id#
</cfquery>
</cffunction>
Since all this method does is update the database with the current data field values in our component, we wont be returning any values.
Our action page that does the update method call
Now that we have our edit form, and our updateJob() method in our job component, all we need to do now is create the action page that performs that update method call.
Below is the code for our page, job_edit_action.cfm
<cfscript>
// create an object to find the job we want to update
search = createObject("component", "job");
// set the id of the job we want to search for
search.id = url.id;
// perform the search and return a results array
result = search.searchJobs();
// set variable "job" to equal the first array element, which
// is of course the job we want to edit.
job = result[1];
// change any of the data properties we want to update
job.name = form.name;
// tell job component to update the database
job.updateJob();
</cfscript>
<!--- return the user to the job listings page --->
<cflocation url="jobs.cfm" addtoken="no">
Testing our results
Now if we run the code, and we don't have any errors, we will have completed the edit / update functionality completely!