ColdFusion-ORM: Define Computed properties (formula attribute)
What are computed properties?
Consider the tables – Art and Artists. Art and Artists form a one-to-many relationship where one Artist has created many Art. Say, in each Artist object, you need a property that would contain the number of arts made by the Artist – use computed properties. The formula to compute the value of the property should be specified using the formula attribute of the cfproperty tag. The formula should be specified as a SQL that returns a single value. Note that computed properties do not map to any column in the table and are not persistent.
Task:
Example to demonstrate using computed properties in ColdFusion-ORM
Previous Related Posts:
Getting Started with ORM
ColdFusion-ORM: Using CRUD Functions
ColdFusion-ORM: Define One-to-Many and Many-to-one relationships
ColdFusion-ORM: Collections
Stuff that you would learn:
- how to define computed properties
- how to define the formula for computed properties
Steps to Run the example:
- This example needs the cfartgallery datasource. This is shipped with ColdFusion by default.
- Create a directory say "ormformula" under webroot.
- Create the following files – Application.cfc, Artists.cfc and index.cfm.
- Run the URL “http://<ip>:<port>/ormformula/index.cfm”
Application.cfc
component
{
//Name of the application
this.name = "ORM_Formula";
//ormenabled should be set to true so that ORM is enabled for this application
this.ormenabled = "true";
//Set the datasource that needs to be used by the ORM Functions. You can also set this in the ormsettings struct
this.datasource = "cfartgallery";
}
Artists.cfc
component persistent="true" entityname="Artists" table="Artists"
{
property name="id" column="ARTISTID" generator="increment";
property name="firstname";
property name="lastname";
property name="address";
property name="city";
property name="state";
property name="postalcode";
property name="email";
property name="phone";
property name="fax";
property name="thepassword";
/*
A property automatically becomes a computed property if formula attribute is specified.
A SQL that returns a single value should be input to the formula attribute. Note
that computed properties do not map to any column in the table and are not persistent.
In this example, the "numberOfArts" property will store the number of arts made by the
Artist.
Have a close look at the SQL that is used. In this SQL, if you specify any column name, by default,
hibernate assumes that the column is from the table to which this CFC maps to - here
Artists table. If you want to specify a column that belongs to another table, you should alias
them. Hence, here, in the where condition art.ArtistId=ArtistId, the latter 'ArtistId' would be
from the Artists table. For each Artists entity, Hibernate would then substitue 'ArtistId' with
the column-value for that entity. For example, for the entity with ArtistId=5, the SQL would be:
select Count(*) from Art art where art.ArtistId=5
*/
property name="numberOfArts" formula="select Count(*) from Art art where art.ArtistId=ArtistId";
}
index.cfm
<!---
This example will teach you
- how to define computed properties
- how to define the formula for computed properties
--->
<cfset ormreload()>
<cfset artists = EntityLoad("Artists")>
<cfloop array="#artists#" index="artist">
<cfoutput>
#artist.getFirstName()# #artist.getLastName()# has <b>#artist.getNumberOfArts()#</b> arts<br>
</cfoutput>
</cfloop>



Recent Comments