472,145 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

Store Friend relations

bilibytes
128 100+
Hi,

i am trying to figure out how to store a friend relation beetween my users.

1.
I thought about using arrays, store for each user an array containing all its friends. But i don't know if arrays are suported in MySQL:
-> if they weren't i could also do a string which is the result of friends concatenation. But maybe it would be to much work to explode and implode the content at each time a user wants to get a list of its friends.


2.
then i thought about using a simple paired relation:
user1 is friend with user2 and the relation status: -1 for friends, -0 for friendship requested
so table would be:
Expand|Select|Wrap|Line Numbers
  1. user1 | user2 | status
  2. paul  | john  | 0
  3. paul  | ron   | 1
  4. jimi  | paul  | 1
  5.  
ok so when the user requests to be friend of an other one, i insert his name in "user1" and the requested friend in "user2"
Example:
paul asked john to be his friend. -> 0 ->friendship not confirmed yet
paul asked ron to be his friend-> 1 ->friendship confirmed, updated 0->1

This would work fine if:
before i insert a friendship request into the table, i look into "user1" coulumn.

Expand|Select|Wrap|Line Numbers
  1. if(there is a record of the requesting user into user1){
  2.      insert into coulumn user1
  3. }
  4. else{
  5.     insert into column 2
  6. }
this would prevent "friends" table from having the same user name(in fact: id) into the 2 different columns, as i have in the Table example above.
-> john is either on user1 and user2 columns.

THE Question is :
isn't that to much work to perform for a simple friend request insertion?

3.
the other possibility which extends this last one, is not to perform the check at insertion, but then check on select into the two columns:

Example, john wants to see his friends:

Expand|Select|Wrap|Line Numbers
  1. SELECT user1 and user2
  2. FROM friends
  3. WHERE user1 = john OR user2 = john 
  4.  
then i should reorder the resultng array..


Experts, what do you think i should do?

Thankyou very much

bilibytes
Nov 14 '08 #1
0 2539

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Iain | last post: by
1 post views Thread by Jef De Rycke | last post: by
1 post views Thread by Randy Fraser | last post: by
5 posts views Thread by G .Net | last post: by
1 post views Thread by ramapriya92 | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.