Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Inserting data with ORM

From: ColdFusion 9 Essential Training

Video: Inserting data with ORM

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.

Inserting data with ORM

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.

Show transcript

This video is part of

Image for ColdFusion 9 Essential Training
ColdFusion 9 Essential Training

95 video lessons · 9714 viewers

David Gassner
Author

 
Expand all | Collapse all
  1. 5m 54s
    1. Welcome
      1m 24s
    2. Using the exercise files
      2m 50s
    3. Overview
      1m 40s
  2. 35m 16s
    1. Understanding application servers
      7m 3s
    2. Installing ColdFusion 9 on Windows
      5m 28s
    3. Installing ColdFusion 9 on Mac OS X
      6m 20s
    4. Completing the Configuration wizard
      1m 50s
    5. Starting and stopping ColdFusion on Windows
      4m 7s
    6. Starting and stopping ColdFusion on Mac OS X
      2m 37s
    7. Installing ColdFusion Builder on Windows
      4m 13s
    8. Installing ColdFusion Builder on Mac OS X
      2m 1s
    9. Setting folder permissions on Mac OS X
      1m 37s
  3. 45m 34s
    1. Using Eclipse perspectives and views
      5m 42s
    2. Defining a ColdFusion server
      8m 42s
    3. Creating a ColdFusion project
      4m 40s
    4. Creating and browsing a ColdFusion page
      5m 36s
    5. Defining a default page template
      3m 58s
    6. Setting and outputting variables
      7m 55s
    7. Using the Snippets view
      4m 49s
    8. Using an external browser
      2m 8s
    9. Using online help
      2m 4s
  4. 35m 48s
    1. Adding numbers and concatenating strings
      8m 7s
    2. Using Debug Output
      6m 10s
    3. Inspecting variables with ‹cfdump›
      4m 54s
    4. Using arrays
      5m 21s
    5. Using structures
      3m 51s
    6. Configuring the line debugger
      1m 25s
    7. Using the line debugger
      6m 0s
  5. 53m 45s
    1. Scripting an Apache Derby database
      3m 34s
    2. Defining a data source in ColdFusion Administrator
      6m 13s
    3. Exploring data sources in RDS Dataview
      4m 2s
    4. Using SQL and the ‹cfquery› tag
      6m 58s
    5. Outputting data from a query object
      4m 2s
    6. Outputting a dynamic HTML table from a query
      7m 0s
    7. Generating alternating row colors
      6m 55s
    8. Creating an application directory and home page
      5m 26s
    9. Displaying data on the home page
      5m 43s
    10. Redirecting requests with ‹cflocation›
      3m 52s
  6. 32m 17s
    1. Including pages with ‹cfinclude›
      4m 21s
    2. Creating reusable header and footer files with ‹cfinclude›
      4m 39s
    3. Creating and calling custom tags
      3m 8s
    4. Passing data into custom tags
      2m 34s
    5. Setting default values for custom tag attributes
      4m 51s
    6. Calling custom tags with ‹cfmodule›
      3m 54s
    7. Using a custom tag as a website header
      3m 55s
    8. Creating an application template file
      4m 55s
  7. 52m 6s
    1. Creating a ColdFusion Component (CFC)
      5m 6s
    2. Declaring CFC functions
      3m 28s
    3. Calling CFC methods with ‹cfinvoke›
      2m 28s
    4. Using CFC properties
      4m 51s
    5. Passing data into functions
      3m 38s
    6. Creating and calling CFCs with script syntax
      5m 25s
    7. Generating data access CFCs in ColdFusion Builder
      4m 35s
    8. Calling generated data access methods
      7m 41s
    9. Creating a dynamic HTML table from an array
      8m 8s
    10. Creating an Application.cfc file
      3m 11s
    11. Using an application data source variable
      3m 35s
  8. 32m 56s
    1. Generating an ORM map component
      7m 58s
    2. Enabling an application to use ORM
      1m 58s
    3. Retrieving data with ORM
      2m 55s
    4. Generating mappings for multiple tables
      4m 22s
    5. Filtering data with ORM
      5m 35s
    6. Displaying result data with a custom tag
      10m 8s
  9. 27m 58s
    1. Passing URL variables in hyperlinks
      6m 13s
    2. Passing multiple URL variables
      2m 50s
    3. Encoding URLs with URLEncodedFormat()
      4m 12s
    4. Checking for available variables with isDefined()
      2m 41s
    5. Creating a data-driven hyperlink
      5m 4s
    6. Displaying selected data in a detail page
      6m 58s
  10. 40m 36s
    1. Using HTML data entry forms
      6m 53s
    2. Processing form variables
      3m 24s
    3. Creating self-posting forms with ‹cfform›
      4m 40s
    4. Creating a search interface
      3m 9s
    5. Processing complex queries with HQL
      8m 53s
    6. Creating a dynamic pulldown list with ‹cfselect›
      7m 16s
    7. Displaying form results
      6m 21s
  11. 43m 46s
    1. Building a data entry form for managing data
      5m 15s
    2. Preparing an ORM mapping for inserting data
      3m 47s
    3. Inserting data with ORM
      5m 23s
    4. Retrieving data for an update form
      4m 16s
    5. Populating form controls with existing data
      5m 2s
    6. Updating data with ORM
      2m 56s
    7. Confirming actions with an Ajax message box
      7m 22s
    8. Using JavaScript callback functions
      3m 15s
    9. Deleting data with ORM
      6m 30s
  12. 9m 37s
    1. Defining client-side form validation rules
      4m 14s
    2. Validating on the server with custom CFML
      5m 23s
  13. 18m 9s
    1. Using the Application.cfc onRequest() method
      4m 12s
    2. Creating and using a login form
      4m 53s
    3. Authenticating users with ‹cflogin›
      6m 1s
    4. Logging out users
      3m 3s
  14. 7m 11s
    1. Configuring an FTP site
      2m 27s
    2. Setting a ColdFusion project's synchronization connection
      2m 17s
    3. Uploading files to the production server
      2m 27s
  15. 46s
    1. Goodbye
      46s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed ColdFusion 9 Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.