ObjectExtender User's Guide and Reference

Maintaining the department to staff relationship

We can now maintain Department and Staff separately. The next step is to allow the relationship to be maintained. Assume that we have a department called 'Geography' and a staff with id of 15. If we want to create a relationship between the two, use the code as follows:

     | dept staff |	
     Transaction begin.
     dept := TstDepartmentHome singleton findByDepartment: 'Geography'.
     staff := StaffHome singleton findByStaffId: '15'.
     staff dept: dept.
     Transaction current commit.
 
 

All we did in the code was to call the department: set method on the staff object. Run the following code

( TstDepartmentHome singleton findByDepartment: 'Geography' ) staff
 
 

Note that the staff will appear in the department's staff. The inverse link of the relationship has been maintained. This is a very powerful feature of ObjectExtender whereby because the relationship was defined to the model browser with the knowledge of both ends of the relationship only one end of the relationship has to be publicly maintained and the other end will be modified automatically. This is also known as relationship handshaking.

To undo the relationship there are therefore two ways for it to be done, either by setting the department to nil on the staff object, or else using the removeStaff: method on the department. The example below uses the removeStaff: method:

     | dept staff |	
     Transaction begin.
     dept := TstDepartmentHome singleton findByDepartment: 'Geography'.
     staff := StaffHome singleton findByStaffId: '15'.
     dept removeStaff: staff.
     Transaction current commit.
 
 

For the user interface we will allow the user to work with a department and add and remove staff members. Modify the TstDepartmentEditView to add two list boxes at the bottom. The left hand list box will show a list of all Staff instances from the StaffHome. The right hand list box will show all staff from the staff relationship attribute of the TstDepartment object. Two buttons allow staff to be added and removed to the TstDepartment.


Figure pgs6b not displayed.

This concludes the relationship example.


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