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>
<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>
|
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. |
None
<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>