You need to load data from a database using ColdFusion 9's new ORM ( Object Relational Mapping ) features.
ColdFusion 9's ORM ( Object Relational Mapping ) features provide easy access to the powerful features of Hibernate. One of the first steps in working with ORM is loading data. ColdFusion provides several functions for working with ORM, one of which is the entityLoad() function, which will be used to load data from a database and place it into entity objects. All of this is done without writing a single line of SQL!
Before using ColdFusion 9's new ORM features, ORM must first be enabled. This is done in the Application.cfc file, by setting three attributes, name, ormenabled and datasource.
<cfcomponent>
<cfset this.name = "wineApp" />
<cfset this.ormenabled = "true" />
<cfset this.datasource = "wine" />
</cfcomponent>
The datasource attribute is also new to ColdFusion 9. By setting this attribute all <cfquery> tags in the application that do not specify a datasource will use the value in the datasource attribute set in Application.cfc.
Once ORM is enababled an entity must be created This is done with a ColdFusion Component (CFC). ORM functionality must be enabled in the CFC by setting the persistent attribute to true. The entity can also be given unique name using the entityname and can be mapped to a an existing table using the table attributes.
<cfcomponent persistent="true" table="wine" entityname="wine"> </cfcomponent>
Each entity must define properties. This is done using the <cfproperty> tag. The name attribute must match the database field name.
<cfcomponent persistent="true" table="wine"
entityname="wine">
<cfproperty name="wineId" />
<cfproperty name="wineVintage" />
<cfproperty name="wineType" />
<cfproperty name="wineRating" />
</cfcomponent>
Data can now be loaded to a variable using the entityLoad() function. The data will be returned as an array of entity objects.
<cfset wine = entityLoad('wine') />
<cfdump var="#wine#" />
The above code will produce the following:
One other ORM function worth noting here that makes the output of data easier is the entityToQuery() function which converts the array of entity objects to a ColdFusion query.
<cfset wine = entityLoad('wine') />
<cfset qWine = entityToQuery(wine) />
<cfdump var="#qWine#" />
The above code will produce the following:
ColdFusion is now outputing data from the database without writing a single line of SQL code!
+