Using Association Tables
QCubed also supports handling many-to-many relationships. Typically, many-to-many relationships are mapped in the database using an Association Table (sometimes also called a Mapping or a Join Table). It is basically a two-column table where both columns are Foreign Keys to two different tables.QCubed allows you to define a set suffix for all Association Tables (the default is "_assn"). Whenever the code generator sees any table that ends in "_assn", it will mark it as a special table to be used/analyzed as an Association Table, associating two objects together in a many-to-many relationship. With the Association Table in place, QCubed will generate five methods each for the two classes involved in this many-to-many relationship. In our example, we created a team_member_project_assn table to represent a many-to-many relationship between Person and Project.
QCubed will generate the following five methods in Person to deal with this many-to-many relationship:
- GetProjectAsTeamMemberArray
- CountProjectsAsTeamMember
- AssociateProjectAsTeamMember
- UnassociateProjectAsTeamMember
- UnassociateAllProjectsAsTeamMember
- GetPersonAsTeamMemberArray
- CountPeopleAsTeamMember
- AssociatePersonAsTeamMember
- UnassociatePersonAsTeamMember
- UnassociateAllPeopleAsTeamMember
Also, similar to our previous example, note that the "AsTeamMember" token in all these methods are there because we named the Association Table in the database team_member_project_assn. If we simply named it person_project_assn, then the methods would be named without the "AsTeamMember" token (e.g. "GetProjectArray", "AssociatePerson", etc.)
Person's Many-to-Many Relationship with Project (via team_member_project_assn)
Person ID: 2First Name: Kendall2
Last Name: Publics
Listing of the Project(s) that This Person is a Team Member of
• ACME Website Redesign
• Blah
• ACME Payment System
There are 3 project(s) that this person is a team member of.
Project's Many-to-Many Relationship with Person (via team_member_project_assn)
Project ID: 1Project Name: ACME Website Redesign
Listing of the Person(s) that This Project has as Team Members
• Kendall2 Publics
• Alex Smith
• Wendy Smith
• Karen Wolfe
• Samantha Jones
There are 5 person(s) that this project has as team members.
