imperium2335 Posted May 8, 2011 Share Posted May 8, 2011 Hi, I am making something where users have profiles and they can add friends. The problem I am having is how to design my database for this, as one user can have many friends and all those friends can have many friends. I know many to many relationships are discouraged but is that the only way this can be done? I am thinking of having a column against each user where the ids of the users the user is friends with are listed i.e. userId userFriendIds 2 1, 13, 15, 63, 106, 253 So in my PHP to return all the friends of user 2, it would pull that big list of ids from the database and if stristr is true then they are friends. What are your thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/235833-user-friends-best-way-to-do-it/ Share on other sites More sharing options...
imperium2335 Posted May 8, 2011 Author Share Posted May 8, 2011 P.S I just thought it could be done in a different table called something like 'friendships'. e.g. userId friendId 1 25 1 207 1 99 2 31 2 207 7 1 Or is this way much more inefficient, btw speed is the most important thing for me, diskspace is irrelevant. Quote Link to comment https://forums.phpfreaks.com/topic/235833-user-friends-best-way-to-do-it/#findComment-1212286 Share on other sites More sharing options...
requinix Posted May 8, 2011 Share Posted May 8, 2011 I just thought it could be done in a different table called something like 'friendships'. That's the correct method. Quote Link to comment https://forums.phpfreaks.com/topic/235833-user-friends-best-way-to-do-it/#findComment-1212289 Share on other sites More sharing options...
imperium2335 Posted May 8, 2011 Author Share Posted May 8, 2011 Thanks, when it comes to indexing ive set the userid to be the primary key, would it be worth making friendid an index for performance gains? It is likely that this table will experience a lot of updates. Quote Link to comment https://forums.phpfreaks.com/topic/235833-user-friends-best-way-to-do-it/#findComment-1212314 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.