When a relationship is defined in a CFC, ColdFusion generates a few methods in the CFC object to add or remove associated objects and to check their existence, for each relationship defined in the CFC.
The generated methods for relationships include:

  • add<relationship_property_name>()}}This method is generated for one-to-many and many-to-many relationships. The method adds the given object to the association collection (array or struct) of the component. For a bidirectional relationship, this method does not set the association on the other end.For{{ type="array", the method signature is:

    add<relationship_property_name>(<associated_object>)

    For type="struct", the method signature is:

    add<relationship_property_name>(<key>, <associated_object>)

  • boolean remove<relationship_property_name>()}}This method is generated for one-to-many and many-to-many relationships. The method removes the object from the associated collection (array or struct) of the component. If the associated object was removed from the collection successfully, then true is returned. For a bidirectional relationship, this method does not remove the association from the other end.For {{type="array", the method signature is:

    boolean remove<relationship_property_name>(<associated_object>)

    For type="struct", the method signature is:

    boolean remove<relationship_property_name>(<key>).

  • {{boolean Has<relationship_property_name>()}}This method is generated for all the relationships. For one-to-many and many-to-many, this method checks whether the association collection is empty. If the association collection is not empty, it returns true. For one-to-one and many-to-one, this method checks whether the associated object exists.
  • boolean Has<relationship_property_name>(<associated_object>)}}This method is generated for one-to-many and many-to-many relationships. The method checks whether the given associated object is present in the association collection. If it is present, it returns true.For {{type="array", the method signature is

    {{boolean has<relationship_property_name>(<associated_object>)}}

    For type="struct", the method signature is

    {{boolean has<relationship_property_name>(<key>)}}

Example

Consider the following example of artists (ARTISTS table) and artwork (ART table), where the artist forms a one-to-many relationship with artwork.Artist.cfc

<cfproperty name="artistid" fieldtype="id"/>
<cfproperty name="firstname"/>
<cfproperty name="lastname"/>
<cfproperty name="state"/>
<cfproperty name="art" fieldtype="one-to-many" cfc="Art" fkcolumn="ArtistID" >
</cfcomponent>

Art.cfc

<cfproperty name="artid" fieldtype="id"/>
<cfproperty name="artname"/>
<cfproperty name="issold"/>
</cfcomponent>

In this example Artist has a relation field art with Art. The following methods are implicitly added to the Artist object:

  • addArts(Art art)
  • booleanremoveArts(Art art)
  • booleanhasArts()
  • booleanhasArts(Art art)
    The attribute singularNameprovides the flexibility to change the name of the generated relationship methods. For example, if the relationship property of Artist is specified as follows:

    <cfproperty name="art" fieldtype="one-to-many" cfc="Art" fkcolumn="ArtistID" singularName="Art">

    then the following methods are generated:

  • addArt(Art art)
  • removeArt(Art art)
  • hasArt()
  • hasArt(Art art)

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy