如何建立友谊关系模型
我一直在试图弄清楚如何做到这一点,即使查看其他示例,我也无法弄清楚,所以也许我可以获得一些个性化的帮助。
我有两个表,users_status
和 friendships
。
在 users_status
表中,我有一个字段 userid
和其他几个字段。 在 friendships
表中,我有字段 request_to
、request_from
和 friendship_status
。
基本上我想要做的是获取当前用户和当前用户的朋友的所有状态帖子(我可以在 PHP 中使用 $userid 变量指定)。
下面是 friendships
表结构的示例。当发送好友请求时,发送者和接收者的 userid 被放入表中,friendship_status 为 0。当请求被接受时,friendship_status 被设置为 1,这两个人现在是好友。
friendship_id request_from request_to friendship_status
1 111248 111249 1
2 111209 111249 1
3 111209 111248 0
11 111209 111259 1
5 111252 111209 1
12 111261 111209 1
我意识到这甚至可能不是确定友谊的最佳结构,特别是因为该网站是基于关系的,并且必须检查友谊连接将是一个经常使用的事情。
对于 friend_requests
和 friendships
有两个单独的表可能会更好吗?如果是这样,我将如何构建/管理friendships
表?
I have been trying to figure out how to do this, and even with looking at other examples, I can't get it figured out, so maybe I can get some personalized help.
I've got two tables, users_status
and friendships
.
In the users_status
table I have a field userid
, and several others.
In the friendships
table, I have the fields request_to
,request_from
, and friendship_status
.
Basically what I want to do is get all of the status posts by the current user AND those who are friends of the current user (which I can specify in my PHP using a $userid variable).
Here's an example of the friendships
table structure. When a friend request is sent, the userid of the sender and receiver are placed in the table, with a friendship_status of 0. When the request is accepted, the friendship_status is set to 1 and those two are now friends.
friendship_id request_from request_to friendship_status
1 111248 111249 1
2 111209 111249 1
3 111209 111248 0
11 111209 111259 1
5 111252 111209 1
12 111261 111209 1
I realize this may not even be the best structure for determining friendships, especially since the site is relationship based and having to check for friendship connections will be a frequently used thing.
Would it perhaps be better to have two separate tables for friend_requests
and friendships
? If so, how would I structure/manage the friendships
table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用表连接(例如 http://dev.mysql.com /doc/refman/5.0/en/join.html)来查找所有请求。
实际上你可以在这里使用子查询:
将
$userid
替换为你的用户 IDYou can use a table join (e.g. http://dev.mysql.com/doc/refman/5.0/en/join.html) to find all of the requests.
Actually you can use a subquery here:
replace
$userid
with your user id我能想到的最简单的模式是:
我还删除了 ID,因为两个表上的两个字段都将是复合主键(request_from、request_to)。
要获取当前用户的所有朋友,只需运行:
这将返回两列,并且您必须在 PHP 中删除当前用户。
从该模式中获取所有朋友的另一种方法是运行 UNION:
该解决方案的缺点是您正在运行 2 个选择
The simplest schema I can think of is:
I also removed the ID because both fields on both tables will be compound primary keys (request_from, request_to).
To get all friends from the current user just run:
This would return both columns and you would have to remove in PHP the current user.
Another way to get all friends from this schema is to run a UNION:
The drawback of this solution is that you're running 2 selects