Orange is my favorite color

OneToMany relationships in Transfer are used when you have a foreign key that references an attribute stored in another table. The example I’m going to give you from my application is Report and Report Type:

tblReport - report parameters like name, owner, status, date
tblReportType - 2-field table of reportTypeID and reportTypeName

My Transfer XML is:

<object name="report" table="tblReport">
<id name="uidreport" type="UUID" />
<property name="reportTypeID" type="UUID" />
<property name="ownerID" type="UUID" nullable="true" />
<property name="reportDate" type="date" />
<property name="reportText" type="string" />
<manytoone name="reportType">
<link column="reportTypeID" to="reportType" />
</manytoone>
</object>
<object name="reportType" table="tblReportType">
<id name="reportTypeID" type="UUID" />
<property name="reportTypeName" type="string"/>
</object>

In the database, tblReportType contains about 8 entries like “Feedback”, “Incident” and “Medical”. These are categories or tags for a report. There is only one report type per report. ManyToOne creates a link to the other object that you can access via get<OtherObjectName>(). In this example, you would use report.getReportType().getReportTypeName(). My simple ColdFusion test:


<cfset obj = transfer.get("member.report", "DDAF460E-E327-E232-75391BC43054B5D8") />
<cfoutput>
#obj.getOwnerID()#
#obj.getReportText()#
#obj.getReportType().getReportTypeName()#
</cfoutput>

Unlike ManyToMany relationships, there is no collection (either structure or array) to access. There are no methods getReportTypeStruct() or getReportTypeArray(). The output comes back as:

ADCF4B3E-E427-F267-753054B5D891BC43
This is a sample report. It could have much more text in it or be empty.
Feedback

In short, the properties of the linked ReportType object are available via the getReportType() method that is now attached to the Report bean and returns a ReportType bean. If instead you had “Person” with a ManyToOne to “Address”, you would use Person.getAddress() to get at the methods of the Address bean. If you had “Payment” with a ManyToOne to “Status”, you would use Payment.getStatus() to get at the methods of the Status bean in your Transfer.xml.

Did you notice this?

<property name="ownerID" type="UUID" nullable="true" />

See the nullable=”true”? This tells Transfer that the field accepts NULLs. Not that surprising, right? What you might not know is that this automatically creates a set<FieldName>Null() method in your bean that will set the field to the value of the NULL for that field type. In this case, I can say:

<cfset obj.setOwnerIDNull() />
<cfoutput>#obj.getOwnerID()#</cfoutput>

The output would be 00000000-0000-0000-0000000000000000 which is the Transfer default for a NULL UUID. Note that these methods don’t exist unless you add nullable=”true” in transfer.xml and restart Transfer.

3 Comments

  1. dickbob said:

    on September 14, 2007 at 8:02 am

    This is good stuff!

    But how do you produce a listing of Reports with their ReportTypes displayed against each? Like in a drill down situation?

  2. brian said:

    on September 15, 2007 at 3:52 pm

    @dickbob – Imagine an HTML table with two columns. As you’re looping over your array of report objects (obtained using the Transfer list() mechanism or a gateway method like getReports(), you would output obj.getReportName() and in the second column obj.getReportType().getReportTypeName() (as defined above in the transfer.xml as reportTypeName). Is that what you’re asking?

  3. dickbob said:

    on September 17, 2007 at 5:48 am

    Thanks, I’ll take a look at that.

{ RSS feed for comments on this post}