Configuring Relationships in Reactor

Reactor can be configured so that it is aware of various types of relationships between objects.  For example, we can easily tell Reactor that a CustomerRecord "has one" AddressRecord by virtue of the addressId column in both tables.

 

Open your reactor.xml config file.  This is what it currently looks like for me:

 

<reactor>
<config>
  <project value="Scratch" />
  <dsn value="Scratch" />
  <type value="mssql" />
  <mapping value="/ScratchData" />
  <mode value="development" />
</config>

<objects/>
</reactor>

 

The Objects section is where you define relationships.  To create the “has one” relationship between a Customer and an Address I add this code inside the objects tag:

 

<object name="Customer">
<hasOne name="Address">
    <relate from="addressId" to="addressId" />
</hasOne>
</object>

 

The object tag identifies a specific object being configured.  In this case I'm configuring the Customer object and saying that it has one Address by virtue of the addressId column in both tables.

 

For details on other configuration objects see the Reactor.xml Configuration File section.

 

My complete config file now looks like this:

 

<reactor>
<config>
  <project value="Scratch" />
  <dsn value="Scratch" />
  <type value="mssql" />
  <mapping value="/ScratchData" />
  <mode value="development" />
</config>

<object name="Customer">
  <hasOne name="Address">
       <relate from="addressId" to="addressId" />
  </hasOne>
</object>
</reactor>

 
Pretty simple, eh?  Rerun the code you created that generated the CustomerRecord and dump the object.  Look at the methods closely.  You will now see these methods on the object: setAddress() and getAddress().
 

If you call getAddress() and dump it like this…

 

<!--- dump this customer's address record --->
<cfdump var="#CustomerRecord.getAddress()#" />

 

…you'll see this dump output:

As you can see, the getAddress() method has returned an AddressRecord object!  The AddressRecord object has methods for each of the fields in the Address table in your database.
 
As of now, I don't have any data in my database.  For this next example I'm going to manually add the following information to the database:
 

Address Table

 

street1

1234 Sample Street

street2

null

city

Happyville

state

New York

zip

54321

 

The database automatically sets 1 as this record’s addressId.

 

Customer Table

 

username

dhughes

password

test

firstName

Doug

lastName

Hughes

addressId

1

 

Now, I'll create a new chunk of code to read the CustomerRecord and outputs the associated address information for that Customer:

 

<!--- create the reactorFactory --->
<cfset Reactor = CreateObject("Component", "reactor.reactorFactory").init(expandPath("reactor.xml")) />

<!--- create a customerRecord --->
<cfset CustomerRecord = Reactor.createRecord("Customer") />

<!--- read customer 1 --->
<cfset CustomerRecord.setCustomerId(1) />
<cfset CustomerRecord.load() />

<!--- get the customer's address record --->
<cfset CustomerAddressRecord = CustomerRecord.getAddress() />

<!--- output the customer's name and address --->
<cfoutput>
   <p>
   <strong>#CustomerRecord.getFirstName()#
   #CustomerRecord.getLastName()#</strong><br />
   #CustomerAddressRecord.getStreet1()#<br />
   <cfif Len(CustomerAddressRecord.getStreet2())>
        #CustomerAddressRecord.getStreet2()#<br />
   </cfif>
   #CustomerAddressRecord.getCity()#,
   #CustomerAddressRecord.getState()#
   #CustomerAddressRecord.getZip()#
   </p>
</cfoutput>

 
When run, this code will load the CustomerRecord, get its AddressRecord and then display the Customer’s address.  The results look like this: