Viewers: in countries Watching now:
In ColdFusion 9 New Features, author and developer Dan Short has gathered together the important updates and improvements in this dynamic web application. Dan showcases the new ColdFusion Builder application throughout the course, using it to work through the new language and CFScript enhancements in this release. He shows how to use the new Server Manager to compare servers and create server groups, as well as monitor server health through notifications. He also explains the new integration with Office applications, building Excel, Word, and even PowerPoint files on the fly from ColdFusion. Finally, he covers the important new Object Relational Mapping feature and how to use the built-in Flash Player. Exercise files accompany this course.
So far we've been dealing with a single object at a time. But in the application world you never have objects that act in isolation. The key to any good application is how it handles related bits of information, using links between tables. In this video we are going to cover how to define foreign key relationships between your objects and how those objects get returned to you when pulling the entities out of the database. So in Chapter5 open up tablerelationships.cfm. Like before, all we are doing is loading a single entity and dumping it out to the screen.
All of our work in this case is going to be done inside of our cfcs. So in order to define relationships, we need to have two things to relate to each right other. Right now we only have an Artist.cfc. Let's create the art component, so I can relate artist and art to each other. In the cfcs folder, right- click and choose New and File. We'll name the file Art.cfc and click Finish. Inside here we'll say cfcomponent persistent = true and that's all we need for our Art.cfc.
Now, you'll notice that I did not specify the table property of the cfcomponent tag. If the component has the exact same name as the table ColdFusion will assume that that's the one you want to be persistent against. So as you see here, Art.cfc has the same name as the Art table. For Artists, the table is named Artists plural but the object is singular. That's why we had to specify the table property of the cfcomponent tag. So let's go ahead and open up Artist.cfc, and we'll declare the relationship in between these two tables.
That's done using the cfproperty tag. So, on the cfproperty tag, I am going to say that the name of this property is Art and I'll specify the fieldtype. In this case, we have a one-to-many relationship. There's one artist to many art pieces. Then I'll specify which key I want to use as the foreign key in the Art table, and in that case it's ArtistID. Next, I need to specify the object that defines the related records.
So I need to specify the cfc Art as the object that is related to Artists. And lastly I'll tell it how I want these related records returned. In this case I want them returned in an array. So when I ask for the art pieces for this artist, I'll get an array of art objects back. We'll save this record, go back to table relationships.cfm, and preview our file, and we get an error. The reason we get an error is because now that I have defined one property for the object, I now need to define all properties that I want to be available.
So let's go back to the Artist object, and we'll define a few more properties. First I need to define the primary key for this particular object, and in that case it's ArtistID. The way I tell it that this is the unique ID for this particular table is I say unique = true, and then that will serve as the primary key for this record. Now I'll specify just a few more properties, so we have something to look at in our examples, but I am not going to define them all because it's not really relevant to how all of this works.
So now I have my Firstname, Lastname, ArtistID and a collection of Art objects. Let's go back to tablerelationship.cfm and reload our page. Now we see I have my ArtistID, Firstname, Lastname and an array of Art objects. If I scroll down here, I can see I have 1, 2, 3, 4, 5 different art pieces, associated with this particular artist. And if I look down here in the Console, I can see that it has run two distinct select statements for me.
The first one defines the Artist ID, Firstname and Lastname. The second select statement shows me all of the art pieces that are assigned with that particular artist. Giving the art pieces back as an array of objects is okay but it's kind of hard to pick out individual art pieces that I want to take a look at or use on my pages. The reason it is difficult to find what I am looking for was an array is I would need to loop through and find the art piece that I would want, and then pull it out and use it. But I can change the way my art pieces get returned to me.
If I go back to the Artist object, I can change my type to struct, and specify I one additional property, which is structkeycolumn, and I'll say I want to use the Art ID as the key column in my structure of art pieces. If I go back to tablerelationships.cfm, I'll reload my page, and now instead of an array I get a struct with the ID as the key column in the structure. So if I wanted to reference Art ID number two, I can say artist.getart and grab the structure with a key of two to get my proper art piece back.
So now that you know how to relate all of your database records, you are ready to go out into the world and can play with ColdFusion 9's new ORM functionality.
There are currently no FAQs about ColdFusion 9 New Features.
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.