Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
ORM stands for Object Relational Mapping, which is a programming technique for converting data in a relational database to a set of objects designed to describe that data. In essence, it means that you have a user object that describes and is built against a user table in your database. The ORM software acts as the map between these two disparate objects and ensures that they stay in sync. In the case of ColdFusion 9, you are in fact using a Java library called Hibernate to take care of this mapping. You can find out more about Hibernate specifically at hibernate.org, but in this chapter, we are going to focus strictly on those features that are exposed directly through ColdFusion.
In this video, we are going to go over the basics of enabling ORM for your application, building a basic ORM enabled CFC, and pulling some records out of the database. So, the first thing to do is to enable ORM for your particular application. By default, all ORM features are disabled to ensure backwards compatibility with existing applications. So, to enable ORM, open up your Application.cfc inside Chapter5 and at the top of the file, we are going to add cfset this.ormenabled = "true".
So, now we have told ColdFusion that for this particular application, we want to use ORM features. But we also need to tell it where we're going to store all of the cfcs that will map to our database. So, to do that we can create a structure into this scope called this.ormsettings. There is a good number of settings that you can declare here, but the only one we are going to worry about for now is where we are going to store all of our CFCs. So, we are going to set our cfclocation to the cfcs folder.
So, let's go ahead and create that folder. We will right-click on Chapter5, choose New > Folder and name it cfcs. Now we need to create a CFC to map against one of our tables. So, inside cfcs folder, we will do New > File and name it Artist.cfc and then click Finish and in our CFC, we will create our cfcomponent tag. I am going to say that this is going to be called Artist. I am going to tell at which table we want the CFC to map to.
In this case, it's going to map to Artists and then I need to say that it is our persistent CFC. So, I set persistent = "true." Believe it or not, this is all I have to do to start using ORM. Let's open up hibernate.cfm and see if we can pull a record out of the database. So, if I want to get all artists inside the Artist table, I will do cfset Artists = EntityLoad, which is a new function in ColdFusion 9. I would tell it which entity I want to load, in this case, our Artist.cfc and then I can dump out my variable.
So, let's take a look at that. And voila, I have an array full of Artist objects. You can see for every record in my database, I have an array node with a full-blown object, with all of the properties. If I expand my Methods, I have SETTHEPASSWORD, SETPOSTALCODE, GETCITY, GETFAX. I have all of the implicit getters and the setters for each of my records. Now that's nice, but I'm honestly used to working with queries in my application. I have a lot of code based off of looping over queries, so getting back an array of objects makes things a little bit complicated if I'm trying to refactor existing code.
If I go back to Safari, we can also use the EntityToQuery function. This will actually take this array of objects and convert it into the standard query I'm used to using inside of my applications. So, let's switch back to our preview. Now we can see we have the same old query that we're used to dealing with in our day-to-day work. The next thing I will show you is how to pull out just a single record from the database. Let's go back to the source code and get rid of our EntityToQuery and after the Artist here, I am going to put some filter criteria.
I only want Artist ID 2 and save our file, preview it and now I've just got that single artist return back to me. Now you will notice that this returned that single record to me inside an array, which isn't very useful. So, if I go back over here to Source, in my EntityLoad function, I can provide a third argument that says I only want to return a single record. If I go back to Safari, you will notice it's returned just the raw object to me outside of an array. Now, I can do more than just return a single record based off of the id.
If I go back into my source code, I can provide a structure of arguments here. Let's get all artists that are in the state of California and preview my page and now I have an array with two artists that are inside the state of California. Now, one more thing to look out for as you're developing your ORM enabled applications is if you make change to your CFC or the table structure, it will not be immediately apparent inside the application. Every time your application starts, ColdFusion caches all of your ORM configuration.
It won't reload it until the application restarts. So to get an ORM configuration reloaded, if you go to your source code, say cfset ORMReload and it will reload the entire ORM application. Also, calling ApplicationStop, which we covered in an earlier video, will also reset the entire ORM application. So now you've learned how to enable your application for ORM, how to reload the ORM settings when necessary and you have created your first persistent CFC and pulled some data out of the database.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 104971 Viewers
56 Video lessons · 116798 Viewers
71 Video lessons · 86010 Viewers
131 Video lessons · 41136 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.