I'm making a PHP / MySQL web app with basic messaging functionality. Two users will have the ability to have a "conversation" which will contain a series of messages.
Here are the model relationships that I conceived:
User > many-to-many > Conversation
Conversation > many-to-many > User AND one-to-many > Messages
Message > belongs-to > Conversations (it could also optionally belongs-to User)
To achieve this, I would need the following tables/fields:
Users
- ID
- ...
- [lots of other fields that don't matter]
- ...
Conversations
- ID
ConversationsUsers
- user_id
- conversation_id
Messages
- ID
- user_id
- conversation_id
- text
As you can see, I'm considering a ConversationsUsers join table to manage the many-to-many relationship between users and conversations.
My questions:
I'm trying to figure out if this is the best way to do this, and two things bother me currently:
It seems inefficient/awkward for the Conversations table to have only one
ID
field whose job is simply to bundle messages. Is there a more elegant way to do this?I need to store some user-specific data about the conversation, such as
is_archived
. In my plan above this is not possible, unless I add those fields to the join table. I think this is knowns as hasMany through in CakePHP, which is the framework I'm using (v2.6.x). I can't think of a way to avoid this, but I'd like to if it's possible because I've found those relationships to be difficult to maintain in the past.
Any other thoughts / feedback about my approach are welcome.