Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Once you've created a data entry form that allows the user to submit values for a database record, you can then insert the data using a very simple bit of code that depends on the Object Relational Mapping features that are new to ColdFusion 9. I am going to start in this demonstration by examining the structure of my photographer table into which I am going to insert data. It has four columns: the FIRSTNAME and LASTNAME column, which are both variable character columns, the FEATURED column, an integer, which I am going to put either a value of zero or one into, and the PHOTOGRAPHERID, a primary key.
In Apache Derby and in a number of other databases such as MySQL and SQL Server, you can set up database table structures that auto-increment their values. That is to say, when you add a new record into a database table, the database assigns the next available value to the primary key, so you don't have to worry about it. You can leverage this functionality using Object Relational Mapping but you have to add a little bit of code to the definition of the structure in your orm file.
I'll go to my photogallery project, to the cfc/orm folder and I'll open the cfc that represents the structure of the Photographer table and I'll double-click the tab to see all of the code. Here is what the code looks like so far. For the primary key column, the name is PHOTOGRAPHERID and the column in the database table is the same, PHOTOGRAPHERID. The ormtype is set to int for integer and the fieldtype is set to id.
If I leave this code as it is, I would have to explicitly assign the PHOTOGRAPHERID value before I insert data, and that's not what I want. Instead I want the underlying Hibernate engine to allow the database to assign the value. To define that, I'll add a new attribute to the cfproperty tag called generator. You'll see that there are a number of values available and if you're curious you can look at the documentation for this attribute to see what's possible. But for these databases that dynamically generate their own identity values I am going to set the value of generator to identity, and then I'll save the change.
In order for ColdFusion to see this change and respect it, I must restart the ColdFusion application. One is to restart the whole server. That takes quite a while. In fact it takes one or two minutes for the entire server to shut down and then restart again. So, I am going to create a file that incorporates a new ColdFusion tag called Application stop. This will be a page that I only call myself, so it's not going to be linked into any of the other parts of the application. I'll go to the photogallery project, I'll right-click on it and select New ColdFusion Page and I'll name the page and appstop and click Finish or press Enter.
Within the body tag I'll create a cfset command and I'll call this new applicationStop method. This will cause the current application to stop on the server and then the next time any page of the application is loaded into a browser that will restart the application and ColdFusion will rescan my cfc folders for my ORM or Object Relational Mapping files and it will reread the mappings and recreate them in memory. I'll finish this page by just outputting an h1 tagset and a bit of literal text 'Application stopped.' I'll run the page in the browser and now the next time I want to use my component, my new rules will take effect.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 92180 Viewers
82 Video lessons · 104056 Viewers
71 Video lessons · 75374 Viewers
56 Video lessons · 106747 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.
Your file was successfully uploaded.