Best way to handle Many-to-Many relationships in PHP MySQL -


i looking best way handle database of many-to-many relationships in php , mysql.

right have 2 tables:

users (id, user_name, first_name, last_name)
connections (id_1, id_2)

in user table id auto incremented on add , user_name unique, can changed. unfortunately, don't have control on user_name , ability changed, must account it.

the connections table obviously, user1 , user2's id.

the connection table needs account these possible relations:

user1 --> user2 (user 1 friends user 2 not user2 friends user1)  user2 --> user1 (user 2 friends user 1 not user1 friends user2)  user1 <--> user2 (user 1 , user 2 mutually friends)  user1 <-!-> user2 (user 1 , user 2 not friends)  

that part not problem, problem having keeping these relations unique when , if change in batches.

possible solution 1: delete of user 1's relations , readd them updated list. think might slow needs.

solution 2? else encounter problem? how should best handle this?

update: distinguishing relationships:

i handle relationships this:

user1, user2 user1, user3 user2, user1 

in example following true:
user1 follows user2 , user3
user2 follows user1 doesn't follow user3
user3 doesn't follow either user1 or user2

you use compound primary key on connections, using both columns (id_1, id_2) if you're having problems uniqueness.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -