We're Just Getting Started!

Reactor has a host of powerful features.  Many of which you will want to use on a day to day basis.  Let's take a look at how we can configure relationships between objects and what sort of power that can give us.

 

Let's say that, for the sake of argument, our Users can purchase something on this website we're building.  We will want to be able to ship whatever they purchased to them.  To facilitate that our users will need to have addresses associated with their account.

 

Let's create a new table called Customer and an Address table.  The customer table will have all the same columns as our User table did, but we'll also give it an additional addressId column which will be a foreign key to our Address table.  

 

Here's what the tables look like:

 

Customer Table

 

Name Type Nullable Default Other
customerId Int No   PK / Identity
username Varchar(50) No    
password Varchar(50) No    
firstName Varchar(50) No    
lastName Varchar(50) No    

dateCreated

DateTime

No

getDate()

 

addressId

Int

No

 

FK to Address.addressId

 

Address Table

 

Name Type Nullable Default Other

addressId

Int

No

 

PK / Identity

street1

Varchar(50)

No

 

 

street2

Varchar(50)

Yes

 

 

city

Varchar(50)

No

 

 

state

Varchar(50)

No

 

 

zip

Varchar(10)

No

 

 

 

In Reactor terminology, the Customer "has one" Address.

 

Note: The address table isn't as normalized as it could be.  This is only an example.

 

Wouldn't it be cool if you could somehow find the customer's address from a CustomerRecord?  Well, you can.

 

Firstly, let's create a CustomerRecord object:

 

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

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

 

If you dump that object you'll see a nice set of methods on it.  In particular, and like the UserRecord, it has getters and setters for every field in the database and the load(), save() and delete() methods.

 

Aside from getAddressId() and setAddressId() there's nothing dealing with addresses though.  Let's do something about that.