CakePHP PrivateMessage 架构和关联

发布于 2024-10-06 17:42:49 字数 757 浏览 0 评论 0原文

我以为我找到了解决问题的方法,但我的方法仍然不能满足我的所有需求。因此我采纳你的建议。这基本上是我想要实现的目标:

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷港 2024-10-13 17:42:49

我想您会想要这样的设置:Users hasMany UserMessage,然后创建一个代表您的 HABTM 表的 UserMessageUsers 模型。在该表中,您将有一个名为 read 的列,它指示收件人是否以及哪些收件人已阅读该邮件。

您可以运行以下一些 SQL 来设置我正在讨论的示例,以便您可以直观地了解该想法。

CREATE TABLE `users` (                                        
  `id` CHAR(36) COLLATE utf8_unicode_ci NOT NULL,             
  `name` VARCHAR(255) COLLATE utf8_unicode_ci DEFAULT NULL,   
  `email` VARCHAR(255) COLLATE utf8_unicode_ci DEFAULT NULL,  
   PRIMARY KEY (`id`)                                          
) ENGINE=MYISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci 

CREATE TABLE `user_messages` (
  `id` CHAR(36) COLLATE utf8_unicode_ci NOT NULL,               
  `user_id` CHAR(36) COLLATE utf8_unicode_ci NOT NULL,          
  `subject` VARCHAR(255) COLLATE utf8_unicode_ci DEFAULT NULL,  
  `body` TEXT COLLATE utf8_unicode_ci,         
  PRIMARY KEY (`id`)          
) ENGINE=MYISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci 

CREATE TABLE `user_messages_users` (                            
  `id` CHAR(36) COLLATE utf8_unicode_ci NOT NULL,               
  `user_message_id` CHAR(36) COLLATE utf8_unicode_ci NOT NULL,  
  `user_id` CHAR(36) COLLATE utf8_unicode_ci NOT NULL,          
  `read` TINYINT(1) DEFAULT '0',                                
  PRIMARY KEY (`id`)                                            
) ENGINE=MYISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci 

我对所有内容都使用 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 hasMany UserMessage and then create a UserMessageUsers model which represents your HABTM table. In that table you would have a column called read 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.

CREATE TABLE `users` (                                        
  `id` CHAR(36) COLLATE utf8_unicode_ci NOT NULL,             
  `name` VARCHAR(255) COLLATE utf8_unicode_ci DEFAULT NULL,   
  `email` VARCHAR(255) COLLATE utf8_unicode_ci DEFAULT NULL,  
   PRIMARY KEY (`id`)                                          
) ENGINE=MYISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci 

CREATE TABLE `user_messages` (
  `id` CHAR(36) COLLATE utf8_unicode_ci NOT NULL,               
  `user_id` CHAR(36) COLLATE utf8_unicode_ci NOT NULL,          
  `subject` VARCHAR(255) COLLATE utf8_unicode_ci DEFAULT NULL,  
  `body` TEXT COLLATE utf8_unicode_ci,         
  PRIMARY KEY (`id`)          
) ENGINE=MYISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci 

CREATE TABLE `user_messages_users` (                            
  `id` CHAR(36) COLLATE utf8_unicode_ci NOT NULL,               
  `user_message_id` CHAR(36) COLLATE utf8_unicode_ci NOT NULL,  
  `user_id` CHAR(36) COLLATE utf8_unicode_ci NOT NULL,          
  `read` TINYINT(1) DEFAULT '0',                                
  PRIMARY KEY (`id`)                                            
) ENGINE=MYISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci 

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 in user_messages_users which links the specific user_messages record to both "Sally" and "Bill"

Now if "Sally" reads the message the specific user_messages_users record will change its read 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文