I thought I came upon a solution to my problem, but still, my approach doesn't fit all my needs. Therefore I resort to your suggestions. Here is basically what I want to achieve:
I have 3 database tables:
- users: id, username, ...
- private_messages: id, ...
- private_messages_users: id, private_message_id, sender_id, recipient_id, status
Viewing this database schema, you will notice that for each message I create a row in the join table, that means if I send a message to two recipients, there will be two inserted rows in the join table. If you have a better different approach, I'm open to suggestions.
I need to define associations in order to:
- be able to compose a message; in the compose form, I would like to have a multiple list from which I can select the recipients I would like to send the message to; the sender is not important here, as I will add it manually into the $this->data array before save
- be able to create an inbox and a sent folder, where I would fetch all messages received, respectively, sent
- be able to view a sent message, together with all recipients it was sent to
- be able to mark a message as read when a user views it for the first time
- also, for each sender_id and recipient_id, I would like to fetch the username from the users table
I would like that all actions requested be completed 'cakily', meaning no 'hacks' or methods to transform arrays. Just plain operations, based entirely on the associations created.
Any help would be highly appreciated, as I am struggling with this problem for over a week. Thank you very much!