ObjectExtender User's Guide and Reference

One-to-many relationship

We will create a model class called TstStaff and make a one to many relationship between TstDepartment and TstStaff. This way a department can have many staff and a staff member belongs to one department.

From the model browser create the TstStaff model. Give it the following attributes:

name String
number Integer
salary ScaledDecimal
title String

To add a relationship between the TstDepartment and TstStaff model classes select the menu bar option Associations and select the menu choice New Associations. Each relationship has a unique name, in this case it can be called departmentStaff . The classes at either end of the relationship are specified as well as the role they have with their counterpart. For example, aTstDepartment knows its Staff instances with the role staff and a Staff object knows its TstDepartment instances with the role department. The role name for the end of a many relationship is usually a plural word, for example, customers, departments, or staff. Our relationship is navigable from both ends in which case the Department will have a collection of Staff instances and each Staff instance can have a department. The 'Role of Staff' within the TstDepartment are set to be many such that a Department can have more than one staff instances. The 'Role Of TstDepartment' is not set to many which implies that it is a one relationship, i.e. a Staff instance can only have one TstDepartment across the relationship Neither end of the relationship is mandatory so the Required check boxes are not set.


Figure pgs6a not displayed.

Having added the relationship the model should be saved and regenerated.

It is important to get good names for the role names as these are used to generate methods. Generating the model will add the methods staff, addStaff: and removeStaff: to the TstDepartment class, and the methods department and department: to the TstStaff class.

Reminder: Whenever you generate or regenerate a model, you will need to generate or regenerate the services code as well.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]