The hasOne tag defines a relationship where a parent object has one of a child object. This relationship is useful in cases where the parent object is related to a child object via a foreign key. To clarify, in your database you may have Customer table and an Address table with the following structure:
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 |
|
|
Without any configuration, an instance of a Customer Record will automatically have getters and setters for each of the fields in the table. However, with a little extra configuration you tell Reactor that your Customer has one Address by virtue of the addressId field in both tables. Here is an example of that configuration:
<reactor>
<objects>
<object
name="Customer">
<hasOne
name="Address">
<relate
from="addressId" to="addressId" />
</hasOne>
</object>
<object
name="Address" />
</objects>
</reactor>
Note: all reactor generated methods with return objects lazy load the object. In other words the Address object is not instantiated or read from the database until you call getAddress(). Additionally these objects are cached. Calling getAddress() two times will return the same instance of the address.
<reactor>
<objects>
<object
name="Customer">
<hasOne
name="Address">
<relate
from="addressId" to="addressId" />
<relate
from="fooId" to="fooId" />
</hasOne>
</object>
<object
name="Address" />
</objects>
</reactor>
User Table
| Name | Type | Nullable | Default | Other |
|---|---|---|---|---|
| userId | Int | No | PK / Identity | |
| username | Varchar(50) | No | ||
| password | Varchar(50) | No | ||
| firstName | Varchar(50) | No | ||
| lastName | Varchar(50) | No | ||
|
dateCreated |
DateTime |
No |
getDate() |
|
Physician Table
| Name | Type | Nullable | Default | Other |
|---|---|---|---|---|
| physicianId | Int | No | PK / FK to User.userId | |
|
licensedState |
Varchar(10) |
No |
|
|
| Name | Type | Nullable | Default | Other |
|---|---|---|---|---|
|
developerId |
Int | No | PK / FK to User.userId | |
|
language |
Varchar(50) |
No |
|
|
|
likesCoffee |
Bit |
No |
1 |
|
<reactor>
<objects>
<object
name="Physician" sharedKey="true">
<hasOne
name="User">
<relate
from="physicianId" to="userId" />
</hasOne>
</object>
<object
name="Developer" sharedKey="true">
<hasOne
name="User">
<relate
from="developerId" to="userId" />
</hasOne>
</object>
<object
name="User" />
</objects>
</reactor>
<!--- create the reactorFactory --->
<cfset Reactor = CreateObject("Component", "reactor.reactorFactory").init(expandPath("reactor.xml"))
/>
<!--- populate the physician --->
<cfset Physician = Reactor.createRecord("Physician") />
<cfset Physician.setLanguage("ColdFusion") />
<cfset Physician.setLikesCoffee(true) />
<cfset Physician.getUser().setUsername("dhughes") />
<cfset Physician.getUser().setPassword("example") />
<cfset Physician.getUser().setFirstName("Doug") />
<cfset Physician.getUser().setLastName("Hughes") />
<!--- save the physician --->
<cfset Physician.save() />
|
Attribute |
Required |
Description |
|---|---|---|
|
name |
yes |
The alias of the related object.
Note: if you did not specify an alias for the object that you are relating to, the alias is defaulted to the object's name. |
|
alias |
no |
The alias to assign to this relationship. When not provided this defaults to the value of the name attribute. |
|
sharedKey |
No |
This indicates if the primary key on this object is related to the primary on the related object. When not provided this defaults to false. |
|
Tag |
Required |
Description |
|---|---|---|
|
yes |
Defines what fields that relate this object to the related object. |
<reactor>
<objects>
<object
name="Customer">
<hasOne
name="Address">
<relate
from="addressId" to="addressId" />
</hasOne>
</object>
<object
name="Address" />
<objects>
</reactor>