Thursday, 19 February 2009

Many to Many Relationships

Over the past few days I've been scratching my head over the competition project. Initially I had decided that a user would just enter their name and email and select a competition from a drop down box... easy right? Then it was suggested to me that there may be multiple open competitions at any one time. In this case ideally the user could select the competitions they want to enter using checkboxes. Still I thought this would be simple but it bought me face to face with many to many relationships.

After much grief and experimentation I was about to give in and go back to plan A when I found this article by Scott Davis. Here he explains how he had been given advice that there is no such thing as a many to many relationship instead there is usually another class that is missed when considering how the objects interact. With this sage like advice I've decided to reassess the competition design.

Now I will use another class (the Entrant) to hold name, email and a set of Entry objects. These entry objects will each map to a competition thereby avoiding the need for a many to many relationship between entries and competitions. Now an entrant can have multiple entries ( 1 to m) and each competition has multiple entries (1 to m).

Stay tuned for news on how this progesses...

No comments: