Adobe 38043740 - ColdFusion Standard - Mac Development Manual page 541

Developing applications
Hide thumbs Also See for 38043740 - ColdFusion Standard - Mac:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
ColdFusion ORM
<cfcomponent persistent="true">
<cfproperty name="artid" generator="increment">
<cfproperty name="artname">
<cfproperty name="price">
<cfproperty name="largeimage">
<cfproperty name="mediaid">
<cfproperty name="issold">
</cfcomponent>
In cfartgallery, the table ARTISTS has a one-to-many relationship with ART table, which are joined using the foreign
key column ARTISTID. This means that each artist has created multiple artwork pieces and many artworks are created
by one artist. To represent this in the object model, each ARTISTS object would contain an array of ART objects. Each
ART object will contain a reference to its ARTISTS object. This is an example of a bidirectional relationship.
To achieve this, you need to add an extra property to the ARTISTS.cfc object that contains the array of ART objects
for that ARTIST.
<cfproperty name="art" type="array" fieldtype="one-to-many" cfc="Art" fkcolumn="ARTISTID">
fieldtype="one-to-many"
is used to convey that the relationship is with "ART" cfc.
CFC="Art"
specifies the foreign key.
fkcolumn="artistid"
ART forms a many-to-one relationship with ARTISTS table because each piece of artwork is created by an artist and
many other pieces of artwork are created by the same artist. To define this relationship, add a property in ART.cfc to
define the relationship with ARTISTS.cfc.
<cfproperty name="artists" fieldtype="many-to-one" fkcolumn="artistid" cfc="Artists"
lazy="true">
fieldtype="many-to-one"
is used to convey that the relationship is with "ARTISTS" cfc.
CFC="ARTISTS"
specifies the foreign key.
fkcolumn="ARTISTID"
Step 5:
Retrieve records in relationship
<cfscript>
artist = EntityLoad("Artists", 1, true);
arts = artist.getArts();
WriteOutput("<b>" & artist.getid() & " " & artist.getfirstname() & " " &
artist.getlastname() & "</b> has " & ArrayLen(arts) & " arts:<br>");
if (ArrayLen(arts) > 0)
{
for(j = 1; j <= ArrayLen(arts); j ++)
{
art = arts[j];
WriteOutput(art.getartname() & "<br>");
}
}
</cfscript>
specifies the type of relation.
specifies the type of relation.
Last updated 1/20/2012
536

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion 9

Table of Contents