Using Association Tables
Qcodo 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.
Qcodo 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, Qcodo 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.
Qcodo will generate the following five methods in
Person to deal with this many-to-many
relationship:
- GetProjectAsTeamMemberArray
- CountProjectsAsTeamMember
- AssociateProjectAsTeamMember
- UnassociateProjectAsTeamMember
- UnassociateAllProjectsAsTeamMember
Qcodo will also generate the following five methods in
Project to deal with this many-to-many
relationship:
- GetPersonAsTeamMemberArray
- CountPeopleAsTeamMember
- AssociatePersonAsTeamMember
- UnassociatePersonAsTeamMember
- UnassociateAllPeopleAsTeamMember
Note that the structure of these five methods are very similar for both objects (get, count,
associate, unassociate, and unassociate all). In fact, you will also notice that this is
the same structure as the reverse one-to-many relationship in our previous example. This especially
makes sense considering that for all three examples, the object is dealing with the "-to-many" side
of the relationship. Regardless if it is a one-"to-many" or a many-"to-many", the five methods
dealing with "-to-many" is consistent.
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: 2
First Name: Kendalll
Last Name: Jenner
Listing of the Project(s) that This Person is a Team Member of
• State College HR Sxzxxystem
There are 1 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: 1
Project Name: ACME Website1
Listing of the Person(s) that This Project has as Team Members
There are 0 person(s) that this project has as team members.