Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Once you've correctly configured your photographer object relational mapping to allow inserting new rows and letting the database handle the creation of the primary key values, you're ready to insert data from the data entry form. I'll reopen the file edit.cfm. This is the file that currently displays a data entry form that lets the user type in a first name and a last name and to indicate whether it's a featured photographer. I'll go back to the source code and I'll place the cursor at the very top of the page.
Because the data entry form is created with the cfform tag, it's a self-posting form. So, when the user first views the page they will see the form, but when they click the Submit button it will repost and will be able to execute action functionality. At the top of the page I'll create a new cfscript section and I'll create a conditional block using the if keyword. I'll ask whether the submit variable exists. I named my Submit button submitButton down here.
So, in the conditional clause, I'll use the isDefined function and I'll look for the variable form.submitButton and I'll add a pair of curly braces. The code within the curly braces will only be executed if the form has posted to itself. In order to add the data to the backend database, I am going to create an instance of my orm component representing the Photographer table. I'll create a variable called photographer and I will initialize it using the complete address of the component photogallery.cfc.orm.Photographer.
Next, I'll set the values of the columns that I want to insert into the data. I'll start with the photographer object's FIRSTNAME. To set each value I'll call the appropriate generated setter method that's a part of the ORM component and I'll pass in the value that the user typed in, form.firstName. I'll do the same thing for the LASTNAME, photographer.setLASTNAME and I'll pass in form.lastName. Make sure you add a semicolon at the end of each line of code.
Now, the checkbox control takes a little bit of extra handling. In ColdFusion when the user checks a check box, you'll receive a form variable that contains the value that was defined in the checkbox control. In my case my cfinput tag has a value of 1, but if the user doesn't check the check box you won't receive any value. To handle this condition use a cfparam tag. I'll place the cfparam tag at the top of the script section, so that it executes each time the page loads before you try to process the action.
cfparam name="form.featured" and default="0". So now, if the variable exists because it has a value of 1, it'll be left alone, but if it doesn't exist, it'll have a value of 0. I'll go down to my script section and add another call to one of the setter methods, photographer.setFEATURED, and I'll pass in form.featured. I now have an object that contains the three values coming from the form.
I am not setting the primary key value because it's going to be generated by the database. So, here's the last step in adding the data to the database. I'll call a method called entitySave and I'll pass in the photographer object. The ORM entitySave method examines the photographer object. If it sees a primary key value, it will update the data in the database and if it doesn't see it, it will insert it. After calling entitiySave I am going to send the user over to the list page, index.cfm, using the location method.
So, the workflow will be once the user types in the values and clicks the button, the page will reload, it will detect that the form was submitted, it will create the photographer ORM object, populate its values and then save that data to the database, and then it will return the user to the list page, showing the user the list of photographers. I'll save that change and go to the list page, index.cfm. This is the version of index.cfm in the photographer's folder.
In this page, I'll place the cursor below the table. This is at about line 31, 32 and I'll create an anchor tag, I'll give it an href of edit.cfm and in between the anchor tags, I'll add the text Insert Photographer. I'll save the change and run the page in the browser, I'll click the link and that'll take me to my form. I'll click into one of the form controls and type in a value. I'll create a new photographer named John Monet and I'll click the Submit button.
And I come back to the list page and there is my new data, added to the database table by the entitySave method of the Object Relational Mapping feature that's new to ColdFusion 9.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 100182 Viewers
56 Video lessons · 113185 Viewers
71 Video lessons · 82055 Viewers
131 Video lessons · 39373 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.