Active Record between two tables with composed PK

Hello,

I am trying to make a system to manage comments and rating by users on a page containing a Flash file.

Here is my DB structure:




User     File     Rating         Comment

----     ----     ------         -------

id (PK)  id (PK)  user (PK, FK)  id (PK)

...      ...      file (PK, FK)  user (FK)

                  value          file (FK)

                                 ...



I would like to add in my comments display the rating the user gave to the file, if there is one (considering an user can rate without adding a comment, and add as many comments as he wants without rating the file).

Is there a simple way to link the rating value to the comment model, considering comment attributes contains both user and file IDs, that compose the PK of the rating?

Thanks.

I’m not sure, but maybe a structure like this




User     File     UserFile      Rating         Comment

----     ----     ---------      ------         -------

id (PK)  id (PK)  user (PK, FK)  id (PK)        id (PK)

...      ...      file (PK, FK)  value          text

                  rating (FK)    ...            ...

                  comment (FK)               



Create an UserFile model, relations:

user belongs-to User

file belongs-to File

rating has-one Rating

comments has-many Comment

/Tommy

So basically it will insert data into UserFile each time a new comment is added, no?

It seems more complicated than adding a rating attribute to each comment, that would generate less requests I think, but I putted this idea aside because I thought it would have been possible for Yii to avoid such data duplication as I asked in my first post.

Thanks for your help.