<link> tag

Description

The relate tag is used to specify an object which related a parent object to a child object.  This tag must be nested under a hasMany tag.  This tag has three arguments, name, from and to.  The name argument specifies the alias of the object through which the parent is linked to the child.

 

Only one link tag can be nested under a hasMany tag.  For detailed information see the hasMany tag documentation.

 

The link tag's from and to arguments are used to specify specific relationships to use to relate the parent to the link and the link to the child. For example, maybe your database structure relates users to other users though a relationship table.  Here's an example schema:

 

User Table

 

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

 

Relation Table

 

Name Type Nullable Default Other
relationId Int No   PK / Identity
userId Int No   FK to User.userId
relatedUserId Int No   FK to User.userId

 

Note that the Relation table will have multiple relationships to the User table though different columns.  Without specifying the from or to attributes your configuration XML would look like this:
 

<reactor>
 <objects>
   <object name="User">
     <hasMany name="User" alias="RelatedUser">
       <link name="Relation" />
     </hasMany>
   </object>

   <object name="Relation">
     <hasOne name="User" alias="ParentUser">
       <relate from="userId" to="userId" />
     </hasOne>
     <hasOne name="User" alias="ChildUser">
       <relate from="relatedUserId" to="userId" />
     </hasOne>
   </object>

 </objects>
</reactor>

 
Given this configuration, reactor will try use to guess which relationships to use to link users to users.  Because there are two relationships from Relation to User this is ambiguous and Reactor will always choose the first one it finds.  This will result in the incorrect data being returned when you call getRelatedUserIterator() on the User Record.
 
To fix the problem you need to be more explicit and define the relationships to use for the link.  In this case, we want to join the User to Relation via the ParentUser relationship and then from the Relation to User via the ChildUser relationship.  This configuration would look like this:
 

<reactor>
 <objects>
   <object name="User">
     <hasMany name="User" alias="RelatedUser">
       <link name="Relation" from="ParentUser" to="ChildUser" />
     </hasMany>
   </object>

   <object name="Relation">
     <hasOne name="User" alias="ParentUser">
       <relate from="userId" to="userId" />
     </hasOne>
     <hasOne name="User" alias="ChildUser">
       <relate from="relatedUserId" to="userId" />
     </hasOne>
   </object>

 </objects>
</reactor>

Attributes

Attribute

Required

Description

name

Yes

The name of the object which links the parent to the child.  (See hasMany for more information.)

from

No

Specifies a specific relationship to relate the parent to the link.  This relationship can be on either the parent or the link.

to

No

Specifies a specific relationship to relate the link to the child.  This relationship can be on either the link or the child.

Child tags

None

Example

<reactor>
 <objects>
   <object name="Customer">
     <hasMany name="Address">
       <link name="Customer" />
     </hasMany>
   </object>

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

   <object name="Address" />
 </objects>
</reactor>