CakePHP PrivateMessage 架构和关联
我以为我找到了解决问题的方法,但我的方法仍然不能满足我的所有需求。因此我采纳你的建议。这基本上是我想要实现的目标:
我有 3 个数据库表:
- users: id, username, ...
- private_messages: id, ...
- private_messages_users: id, private_message_id, sender_id,recipient_id, status
查看此数据库架构,您会注意到对于每条消息,我在连接表中创建一行,这意味着如果我向两个收件人发送消息,连接表中将有两个插入行。如果您有更好的不同方法,我愿意接受建议。
我需要定义关联以便:
- 能够撰写消息;在撰写表单中,我希望有一个多个列表,我可以从中选择我想要向其发送消息的收件人;发件人在这里并不重要,因为我将在保存之前将其手动添加到 $this->data 数组中
- ,以便能够创建收件箱和发送文件夹,在其中我将分别获取所有收到的消息,发送的消息
- 能够查看已发送的消息及其发送的所有收件人,以便
- 在用户第一次查看消息时能够将消息标记为已读
- 从用户表中获取用户名
,对于每个 sender_id 和recipient_id,我想 就像所有请求的操作都“cakily”完成一样,这意味着没有“黑客”或转换数组的方法。只是简单的操作,完全基于创建的关联。
任何帮助将不胜感激,因为我在这个问题上挣扎了一个多星期。非常感谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想您会想要这样的设置:
Users
hasManyUserMessage
,然后创建一个代表您的 HABTM 表的UserMessageUsers
模型。在该表中,您将有一个名为read
的列,它指示收件人是否以及哪些收件人已阅读该邮件。您可以运行以下一些 SQL 来设置我正在讨论的示例,以便您可以直观地了解该想法。
我对所有内容都使用 UUID,因此使用
CHAR(36)
列。因此,假设一个用户“Bob”向“Sally”和“Bill”发送了一封电子邮件,其中将是放入
user_messages
中的一条记录,该记录链接回作为创建者的“Bob”。然后,user_messages_users
中将有两条记录,将特定的user_messages
记录链接到“Sally”和“Bill”。现在,如果“Sally”读取了特定
的消息user_messages_users
记录会将其read
列更改为 1 - 表示她已阅读该消息。 “Bob”的记录将保持为 0——表明他还没有读过。要对 HABTM 表执行更改,您需要为其创建一个模型。 CakePHP 书 讨论了这一点,特别是
with
关系的选项。I'd imagine that you would want a setup like this:
Users
hasManyUserMessage
and then create aUserMessageUsers
model which represents your HABTM table. In that table you would have a column calledread
which indicates if, and which, recipient had read the message.Here's some SQL that you can run to setup an example of what I'm talking about so you can visualize the idea.
I use UUIDs for everything, hence the
CHAR(36)
columns.So lets say a user, "Bob" sends an email to "Sally" and "Bill" there will be one record put into
user_messages
which links back to "Bob" as the creator. Then there will be two records inuser_messages_users
which links the specificuser_messages
record to both "Sally" and "Bill"Now if "Sally" reads the message the specific
user_messages_users
record will change itsread
column to 1 -- indicating that she has read the message. "Bob"s record will remain 0 -- to indicate that he has not read it.To perform the changes on the HABTM table you will need to create a model for it. The CakePHP book talks about this, specifically the
with
option for relationships.