Adobe COLDFUSION 9 Manual page 588

Developing applications
Hide thumbs Also See for COLDFUSION 9:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
ColdFusion ORM
Note: In case of more than one parameter, values are picked up based on the parameter sequence, for example, the first
parameter will be replaced by first value and second parameter will be replaced by second value.
Examples: named parameters
This type of
ORMExecuteQuery
be a name and should start with ":" as in ":age" or ":id". The values to the names should be passed as key-value pairs.
For example, to retrieve artist details of all artists whose reside in USA and are also citizens of USA, your code should
look like this:
<cfset USArtists = ORMExecuteQuery("from ARTIST where country=:country and
citizenship=:country", {country='USA'})>
<cfset orderDetail = ORMExecuteQuery("from Orders where OrderID=:orderid and
ProductID=:productid", {orderid=1, productid=901}, true)>
Note: Parameters are not case-sensitive.
Examples: group by
This type of ORMExecuteQuery lets you retrieve aggregate or grouped values for the query.
For example, to retrieve the first name and last name along with the status of the artwork being sold or not, you can
write a query similar to the following:
<cfset artist = ORMExecuteQuery(
"SELECT art.Artist.Firstname, art.Artist.Lastname, SUM(art.Price) as Sold FROM
Art as art WHERE art.IsSold=1 GROUP BY art.Artist.Firstname, art.Artist.Lastname")>
<cfloop array="#artist#" index="artistItem">
<cfoutput>
#artistItem[1]# #artistItem[2]# #artistItem[3]#<br>
</cfoutput>
</cfloop>
Note: Built-in functions to obtain the data such as getFirstName() or getLastName() cannot be used if you are using select
queries with specific column names. The result will be returned as an array object and values can be retrieved using array
index.
Example: order by
This type of ORMExecuteQuery lets you retrieve sorted data from a data source using the order by clause. For example,
to sort the data from the Artist table by firstname, use the following code:
<cfset artist = ORMExecuteQuery('FROM Artist ORDER BY firstname ASC', false, {maxresults=5} )>
<cfloop array="#artist#" index="artistObj">
<cfoutput>Name = #artistObj.getFirstName()#
#artistObj.getLastName()#<br></cfoutput>
<br>
</cfloop>
Example: aggregate functions
This type of
ORMExecuteQuery
<cfset artist = ORMExecuteQuery(
"SELECT COUNT(*) FROM Art as art WHERE art.Artist.ArtistID=:ArtistID AND
art.IsSold=:Sold", { ArtistID=1, Sold=True }, True )>
<cfoutput>
#artist#
</cfoutput><br>
lets you pass named parameters to the query. The placeholder for the parameter should
lets you retrieve data when using aggregate functions such as sum, count, avg.
Last updated 8/5/2010
583

Advertisement

Table of Contents
loading

Table of Contents