mysql - How to handle a bidirectional many-to-many relationship with PHP -
i trying setup bidirectional many-to-many relationship using same table data, namely user, have link table named userusers joins 1 user user, not sure how handle bidirectional side, because result of code shows 1 direction.
class="lang-sql prettyprint-override">--table user: create table user ( userid int auto_increment not null, userfirstname varchar(30) not null, usersurname varchar(30) not null, usertel char(10), usercell char(10), useremail varchar(50) not null, userpassword varchar(50) not null, userimage varchar(50), useraddress1 varchar(50), useraddress2 varchar(50), usertown/city varchar(50), userprovince varchar(50), usercountry varchar(50), userpostalcode varchar(50), primary key(userid) ) --table userusers: create table userusers ( userid int not null, friendid int not null, primary key(userid, friendid), foreign key(userid) references user(userid), foreign key(friendid) references user(userid) )
php code:
$sql="select * user u inner bring together userusers uu on uu.userid = u.userid inner bring together user f on f.userid = uu.friendid uu.userid = " . $_session['userid'];
actually, no, tables show bidirectional relationships.
consider table usersusers
the column userid represents person friends friendid.
say user 1 wants friends user 2
our usersusers table like:
userid | friendid 1 2
sure, see there relationship between user 1 , 2, see user1 has initiated friendship, showing direction 1 -> 2
now when user2 wants take friendship, can insert record table, making it:
userid | friendid 1 2 2 1
there's improve meta info can add together relationship table understand more relationship created, userid field should considered owner
, or sender
, , friendid should considered receiver
.
so - have of work cutting out you, have create queries deed upon bidirectional relationship.
getting confirmed friends user1:
we assume $the_logged_in_user_id = 1
select * users u bring together usersusers f on u.userid = f.userid f.friendid = '$the_logged_in_user_id'
that show confirmed friendships $the_logged_in_user_id
, assuming you're going for.
php mysql
No comments:
Post a Comment