简体   繁体   English

PHP-消息收件箱系统

[英]PHP - Messaging inbox system

I am developing a messaging inbox system. 我正在开发一个消息收件箱系统。 Users create message threads directed at another user. 用户创建针对另一个用户的消息线程。 It will work as follows: 它将如下工作:

  1. Message threads you receive appear in your inbox. 您收到的消息主题将显示在收件箱中。
  2. Message threads you create also appear in your inbox. 您创建的消息线程也会显示在收件箱中。
  3. Message threads you delete are only deleted from your own inbox. 您删除的邮件主题只会从您自己的收件箱中删除。

I have the following fields in my message_thread table: 我的message_thread表中包含以下字段:

  • id ID
  • from_user_id from_user_id
  • to_user_id to_user_id
  • subject 学科
  • deleted 删除
  • updated_at 的updated_at

I have managed to implement 1 and 2 using the following query: 我设法使用以下查询实现了1和2:

SELECT * FROM message_thread mt
WHERE ((mt.from_user_id = 1 OR mt.to_user_id = 1) AND mt.deleted = 0)
ORDER BY mt.updated_at DESC

I am trying to figure out a way of implementing 3. 我正在尝试找出一种实现3的方法。

I assume new columns will need to be introduced ( from_user_deleted and to_user_deleted ). 我假设将需要引入新的列( from_user_deletedto_user_deleted )。 Is it possible to retrieve the correct resultset by extending the query or will this need to be done in the server side script (currently using PHP)? 是否可以通过扩展查询来检索正确的结果集,或者这需要在服务器端脚本中完成(当前使用PHP)?

EDIT: If user A deletes a thread and then user B sends a reply to that thread, it should re-appear in user A's inbox. 编辑:如果用户A删除一个线程,然后用户B发送对该线程的答复,它应该重新出现在用户A的收件箱中。

Try below code: 试试下面的代码:

1.You can add new field as status 1.您可以添加新字段作为状态

2.Status 2.Status

    1 = from_user_deleted
    2 = to_user_deleted

Then Query 然后查询

SELECT * FROM message_thread mt
WHERE ((mt.from_user_id = 1 OR mt.to_user_id = 1) AND mt.deleted = 0 AND mt.status<>2)
ORDER BY mt.updated_at DESC

In plain english: 用简单的英语:

Where ( The message is to you OR ( the message is from you AND the message is not deleted ) ) 哪里(消息是给您的,还是(消息是来自您的,并且消息不会被删除))

I have managed to solve this by adding a new table: 我设法通过添加一个新表来解决此问题:

message_thread_status

  • message_thread_id (FK) message_thread_id(FK)
  • user_id (FK) user_id(FK)
  • deleted (default 0) 已删除(默认为0)

When a message_thread is created, it creates two entries in the message_thread_status table (one for each user in the message thread). 创建message_thread ,它将在message_thread_status表中创建两个条目(对于消息线程中的每个用户一个条目)。

I then just need to find the record for the current user in the message_thread_status table and add in a condition to check for the value of deleted . 然后,我只需要在message_thread_status表中查找当前用户的记录,并添加一个条件以检查deleted的值。

New query: 新查询:

SELECT
     * FROM message_thread mt

LEFT OUTER JOIN
     message_thread_status mts ON mts.message_thread_id = mt.id

WHERE
    ((mt.from_user_id = 1 OR mt.to_user_id = 1)
    AND (mts.user_id = 1 AND mts.deleted = 0))

ORDER BY
    mt.updated_at DESC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM