PHP - 添加用户为好友

发布于 2024-10-08 04:22:08 字数 285 浏览 1 评论 0原文

我有一个社交网站,你可以添加朋友。它的工作方式是这样的:

  • 点击添加好友
  • 添加点击进入好友的用户 数据库行,就像列中的这样 [bob][fred][123123]
  • fred 在验证中接受它并包含秘密 链接,通过电子邮件。
  • 然后将另一行放入 数据库反过来,就像 [fred][bob][123123]

然后我可以确定他们是否是朋友以及他们是否已被接受。

然而,这肯定不是正确的方法。我现在非常不确定哪种方法可以更好并且运行更顺利。

I have a social website, you can add friends. The way it works is like this:

  • click add friend
  • add the user who clicked into a
    database row, like this in the columns
    [bob][fred][123123]
  • fred accepts it with the secret in a verification
    link, via email.
  • this then puts another row in the
    database the other way round, like
    this [fred][bob][123123]

I can then determine if they are friends or not and if they have been accepted.

However, this surely isn't the correct way. I am now very unsure of a way of which could be better and run more smoothly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

风吹雪碎 2024-10-15 04:22:08

实际上没有正确或错误的方法,这取决于您决定使用什么,最终它是您的代码,所以无论您选择什么,都适合您。

然而,你是完全正确的,输入两行将是一个非常沉重的开销,并且使用额外的空间似乎没有充分的理由。更简单的是,您可以在数据库中设置另一列:

user_1 | user_2 | accept_code | accepted

user_1 请求将 user_2 添加为好友 - 您设置一个 accept_code 在数据库中创建一个条目。设置数据库结构,将 accepted 列设置为 define as false。然后,当首次创建该行时,用户当前不是朋友。

使用您的示例:bob 请求 Fred 作为朋友。您的数据库现在看起来像这样:

bob |弗雷德 | 弗雷德123123 | false

user_2输入accept_code时,将accepted更改为true。

鲍勃 |弗雷德 | 弗雷德123123 | true

这样,一个查询将告诉您这两个用户是否是朋友,而不是两个查询来查看是否有两个匹配的数据库条目。

例如,鲍勃已将弗雷德、乔和亚历克斯添加为朋友,弗雷德和亚历克斯已接受鲍勃为朋友,但乔还没有。您的数据库将如下所示:

user_1 | user_2 | accept_code | 已接受

bob |弗雷德 | 弗雷德123123 | true

bob | | 321321 |

鲍勃 |亚历克斯 | 亚历克斯789789 | true

因此,例如,一个伪选择可能会找到 bob 的所有朋友:

SELECT user_2 FROM 关系 WHERE user_1="bob" AND Accepted="true"

结果将是:

弗雷德
alex

根据评论更新:

数据库结构:

user_1 | user_2 | accept_code | 已接受

bob |弗雷德 | 弗雷德123 | true

bob |亚历克斯 | 亚历克斯123 | true

bob | | 123 |

ste | 鲍勃 | 123 | true

|亚历克斯 | 亚历克斯123 | true

Select 语句:

SELECT * FROM 关系 WHEREaccepted = 'true' AND (user_1 = 'current_user' OR user_2 = 'current_user');

示例 1 - bob 登录,他请求加好友并被请求加为好友:

SELECT * FROM 关系 WHERE Accepted = 'true' AND (user_1 = 'bob' OR user_2 = 'bob ');

结果:

bob |弗雷德 | 弗雷德123 | 已接受

bob |亚历克斯 | 亚历克斯123 | 已接受

ste | 鲍勃 | 123 | accepted

示例 2 - alex 登录,他从未请求加为好友,但已被请求作为好友:

SELECT * FROM 关系 WHERE Accepted = 'true' AND (user_1 = 'alex' OR user_2 = 'alex');

结果:

bob |亚历克斯 | 亚历克斯123 | 已接受

|亚历克斯 | 亚历克斯123 | 已接受

示例 3 - joe 登录,他请求添加好友并拒绝了好友:

SELECT * FROM 关系 WHERE Accepted = 'true' AND (user_1 = 'joe' OR user_2 = 'joe');

结果:

joe |亚历克斯 | 亚历克斯123 | 已接受

There is really no right or wrong way, it is what ever you decide to use, at the end of the day it is your code, so whatever you choose and works for you.

However, you are quite correct, entering two rows would be a very heavy overhead and use extra space for seemingly no good reason. A lot more simply you could set another column in your DB:

user_1 | user_2 | accept_code | accepted

user_1 requests to add user_2 as a friend - you set an accept_code creating an entry in the DB. Set your DB structure to set the accepted column to define as false. Then when the row is first created the users are not currently friends.

Using your example: bob requests fred as a friend. You DB would now look like this:

bob | fred | 123123 | false

When user_2 enters the accept_code, then change accepted to true.

bob | fred | 123123 | true

This way, one query will tell you if the two users are friends, instead of two queries to see if you have two matching DB entries.

So for example, bob has added fred, joe and alex as friends, fred and alex have accepted bob as a friend, but joe has not. You DB would look like this:

user_1 | user_2 | accept_code | accepted

bob | fred | 123123 | true

bob | joe | 321321 | false

bob | alex | 789789 | true

So for example, a psuedo select maybe, find all friends for bob:

SELECT user_2 FROM relationships WHERE user_1="bob" AND accepted="true"

The result would be:

fred
alex

UPDATE as per the comments:

DB Structure:

user_1 | user_2 | accept_code | accepted

bob | fred | 123 | true

bob | alex | 123 | true

bob | joe | 123 | false

ste | bob | 123 | true

joe | alex | 123 | true

Select statement:

SELECT * FROM relationships WHERE accepted = 'true' AND (user_1 = 'current_user' OR user_2 = 'current_user');

Example 1 - bob logs in, he has requested friends and been requested as a friend:

SELECT * FROM relationships WHERE accepted = 'true' AND (user_1 = 'bob' OR user_2 = 'bob');

The result:

bob | fred | 123 | accepted

bob | alex | 123 | accepted

ste | bob | 123 | accepted

Example 2 - alex logs in, he has never requested and friends but has been requested as a friend:

SELECT * FROM relationships WHERE accepted = 'true' AND (user_1 = 'alex' OR user_2 = 'alex');

The result:

bob | alex | 123 | accepted

joe | alex | 123 | accepted

Example 3 - joe logs in, he has requested a friend and has declined a friend:

SELECT * FROM relationships WHERE accepted = 'true' AND (user_1 = 'joe' OR user_2 = 'joe');

The result:

joe | alex | 123 | accepted

朦胧时间 2024-10-15 04:22:08

社交网络数据库关系可能会很快变得非常复杂,尤其是当流量扩大和请求涌入时。Facebook 等领先来源在其开发者博客中提供了大量数据,介绍他们如何修改和调整系统以适应其规模。交通量增加。

没有任何一种正确的方法来创建关系 - 这取决于您的代码结构 - 但是据说执行两个查询来创建简单的一对一关系并复制数据被正确地认为是不必要的额外开销。

您可以使用两个用户的 id 创建一个简单的联结表,一个字段保存发起请求的人(用户的 int 主键),另一个字段保存接收批准请求的字段(也是 int 主键)。还有另一个 status int 字段,其数值对应于关系的状态。

您应该始终使用数字键来表示您的用户身份 - 您永远不知道有多少“约翰”会加入!确保您只有一个简单的“用户”表,其中的用户 ID 是唯一键。

UserID|Name
1      Frank
2      John

这当然可以包含其他信息,姓氏,位置,性别,最喜欢的西尔维斯特·史泰龙电影等。

可以在具有以下结构的单独关系表中创建关系......因此,当弗兰克(id 1)请求约翰(id 2)时,我们最终得到这样的一行。

RelationshipID|RequestedBy|Receipient|RequestStatus|ConfirmationCode
(primary key)       1           2        0            (however you format your url strings)

然后您就可以拥有所有关系的主键。每个单独的链接都是可识别的。

另外,我个人会集成另一个表来定义状态变量。

StatusIdentifier|StatusDefinition
0                 Requested but not yet approved
-1                Requested and denied (mark for deletion)
1                 Requested and approved (active friendship)

通过这种方式,如果您想要更多关系,您可以扩展程序中的关系类型,例如加好友然后删除、阻止等。在我部署的大多数应用程序中,这种数据是 XML 的良好候选者,但是有将其放入数据库中没有任何问题。接收者的响应必须获取RelationshipID,然后更新此状态值。

在关系表上添加更新时间戳也是一个好主意。

使用此结构,您可以使用用户表中的主 ID 轻松查询所有用户的朋友。让我们将 Franks 放入一个数组中:

$sql = 'SELECT Recipient FROM FriendRelationships WHERE RequestedBy = 1 AND RequestStatus = 1';

这将获取 Frank 的所有活跃好友用户 ID,因为他是请求者 (1),并且我们仅限于已由接收者批准的请求。

Social networking database relations can become VERY complicated quickly, especially as the traffic scales up and requests flood in. There is great data available out there from the leading sources like Facebook in their developer blogs on how they modified and adjusted their systems to scale as their traffic increased.

There isn't any one right way to create the relationship - it depends on your code structure - but that being said doing your two queries to create a simple one to one relationship and duplicating the data is correctly assumed to be needless extra overhead.

You could create a simple junction table with the ids of the two users, one field holding whom originated the request (int primary key of the user) and the other the one receiving the request for approval (also the int primary key.) You could have another status int field with numerical values that correspond to the status of the relationship.

You should always use numerical keys for your user's identities - you never know how many 'John's will join up! Make sure you have even just a simple 'Users' table of sorts with UserIDs that are unique keys.

UserID|Name
1      Frank
2      John

This could of course contain other info, last name, location, gender, favorite Sylvester Stallone movie etc.

Relationships can be created in a separate relationships table with the following structure... So when Frank (id 1) requests John (id 2), we end up with a row like this.

RelationshipID|RequestedBy|Receipient|RequestStatus|ConfirmationCode
(primary key)       1           2        0            (however you format your url strings)

You could then have a primary key for ALL the relationships. Every individual link would be identifiable.

Also, I personally would integrate another table to define the status variable.

StatusIdentifier|StatusDefinition
0                 Requested but not yet approved
-1                Requested and denied (mark for deletion)
1                 Requested and approved (active friendship)

This way you could extend the types of relationships in the program if you ever wanted more, such as friended then deleted, blocked, etc. In most of the apps I've deployed this kind of data was a good candidate for XML, but there's nothing wrong with throwing it in a database. Responses by the recipient would have to get the RelationshipID and then update this status value.

Adding a timestamp for updates on the relationship table is a good idea too.

Using this structure, you could easily query all the user's friends using their primary id from your user table. Let's get Franks into an array:

$sql = 'SELECT Recipient FROM FriendRelationships WHERE RequestedBy = 1 AND RequestStatus = 1';

This gets all active friend user ids for Frank, since he is the requester (1) and we're limiting to requests that have been approved by the recipient.

遇见了你 2024-10-15 04:22:08

你好,你可以试试这个逻辑。

假设鲍勃想添加弗雷德为好友。

  1. bob 发送 fred 添加为好友

    操作:有将数据库查找表作为好友:[bob] [fred] [隐藏-仅当 bob 接受时才可见]

  2. fred 验证后接受请求
    行动:使[隐藏] ---> [可见]

    现在您与用户和朋友建立了关系。[查找表中的每一行都是朋友]

    希望这会有所帮助

hi you can try this logic.

Lets say bob wants to add fred as friend.

  1. bob send fred to add as friend

    Action: there is will database lookup table as friends: [bob] [fred] [hide-only make it visible when bob accepts it]

  2. fred accepts the request after validation
    Action: make the [hide] ---> [ visible]

    Now you have relation with user's and friends.[each row in lookup table is friends]

    hope this will be helpful

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