
536
DEVELOPING
COLDFUSION 9 APPLICATIONS
ColdFusion ORM
Las
t
upda
te
d 8/5/2010
<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"
specifies the type of relation.
CFC="Art"
is used to convey that the relationship is with "ART" cfc.
fkcolumn="artistid"
specifies the foreign key.
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"
specifies the type of relation.
CFC="ARTISTS"
is used to convey that the relationship is with "ARTISTS" cfc.
fkcolumn="ARTISTID"
specifies the foreign key.
Step 5:
Retrieve records in relationship
<cfscript>
artist = EntityLoad("Artists", 1, true);
art = artist.getArts();
WriteOutput("<b>" & artist.getid() & " " & artist.getfirstname() & " " &
artist.getlastname() & "</b> has " & ArrayLen(art) & " art:<br>");
if (ArrayLen(art) > 0)
{
for(j = 1; j <= ArrayLen(art); j ++)
{
art = art[j];
WriteOutput(art.getartname() & "<br>");
}
}
</cfscript>