Reactor.xml: Understanding the difference between 'relate' and 'link'
One of my favorite features of reactor (besides everything) is the ability to set up hasOne and HasMany relationships in your reactor.xml file and then be able to use iterators and get other related records and data from the record you are working with. setting up these relationship seems to be an area of confusion for people as they get started with reactor.
Relationships:
There are two basic relationship types: hasOne and HasMany to decide from. this should be an easy task if you already have a basic plan for the app. It may help if you imagine yourself as the object...I am a user object and I 'have many' roles or I am a user and I 'have one' address.
Relationship Types:
Inside the hasOne or hasMany tag, you can define the relationship type.
There are two basic types of reactor.xml relationship types, Reactor syntax calls these 'relate' and 'link'
This is HOW the objects are related. Either a direct relationship(relate) or through a linking table (link)
relate
This is when the Primary key(PK) of one table is used as a foreign key in a second table. This is the one to use if you are only involving 2 tables -with no linking table in the middle. In the below example the 'UserID' PK in the User table is a foreign key in the Reports table. In this instance the user 'Has Many' Reports using a relate tag:
<object name="User" >
<hasMany name="Report">
<relate from="UserID" to="UserID" />
</hasMany>
</object>this is the easiet to set up in reactor as you just need only define the two objects with a
link
the 'link' syntax is for when you have a table in the middle like in the case of a user table, a role table and a table that links userID's to RoleID's (UserRole table) This requires three reactor object definitions. in this example: User, Role and UserRole
the user and role both 'have many' of each other:
<object name="User">
<hasMany name="Role">
<link name="UserRole" />
</hasMany>
</object>
<object name="Role">
<hasMany name="User">
<link name="UserRole" />
</hasMany>
</object>The linking table (UserRole) has 2 hasOne definitions, relating back to the 2 PK's
<object name="UserRole">
<hasOne name="User">
<relate from="UserID" to="UserID" />
</hasOne>
<hasOne name="Role">
<relate from="RoleID" to="RoleID" />
</hasOne>
</object>
delicious |
digg |
reddit |
technorati
