I’ll assume here that you have already set ORM in your Application.cfc. If not you may want to check out the post on setting up ORM in the Application.cfc file first. If you have the Application file all set and you have a database then you’re ready to map out a database table.
For this example I’ll be using MySql as the database with a table called ‘stores_tbl’. It has three fields that we need to worry about. The first is the ‘store_id’ (the primary key), the ‘store_name’, and the ‘store_link’.
We’ll need three files to make this work. First is the Application.cfc file, which again I’m assuming is already set up. Second is a .cfm file that will output our data objects. And third is the persistent cfc that will map to the ‘stores_tbl’. I’ve created a directory called ‘orm_mappings’ under my root directory to hold all my persistent cfcs. You can also create a directory if you like. When the application loads Coldfusion searches through the application looking for any persistent cfcs and maps them accordingly. So technically you could organize your persistent cfcs any way you wish as long as they are inside the application using ORM.
Let’s take a look at our persistent cfc.
stores_tbl.cfc
<cfcomponent displayname="Stores Table" hint="ORM mappings to the stores_tbl" output="false" persistent="true"> <!--- SET PRIMARY KEY COLUMN ---> <cfproperty name="id" column="store_id" ormtype="integer" generator="native"> <!--- SET OTHER COLUMNS ---> <cfproperty name="store_name" ormtype="string"> <cfproperty name="store_link" ormtype="string"> </cfcomponent>
First thing to note here is the name of the cfc. It is named the same as the table name it is mapping to. So our table name is ‘stores_tbl’ and the name of the cfc is ‘stores_tbl.cfc’. The name of the cfc will indicate to Coldfusion where the mapping is to take place. Next is the ‘persistent=”true”‘ attribute in the ‘cfcomponent’ tag. This indicates to coldfusion that this is an ORM mapping and not a standard cfc.
At Monserrate Monastery in Bogotá, Colombia.