framework icon indicating copy to clipboard operation
framework copied to clipboard

[Eloquent][Proposal] BelongsToManySymmetrical

Open rulrok opened this issue 11 years ago • 13 comments

The motivating problem can be found at issue #2673 . I've made a long comment there. Since the comment got bigger and bigger, and because the topic is very old, I felt motivated to open a new issue better describing my ideas here. On that new issue I summarize what a described there.

Motivation

Add/remove friends/followers in an eloquent manner from both ways (either as the follower or as the followed).

Description of problem

For those situations, we have a many-to-many relationship similar to this:

user_id friend_id
1 2
1 3
2 3
2 5

The following methods would be reasonable.

 public function friends(){
   return $this->belongsToMany('App\User', 'user_friend', 'user_id', 'friend_id');
 }

 public function add_friend(User $user) {
   $this->friends()->attach($user);
   return $this;
 }

 public function remove_friend(User $user) {
   $this->friends()->detach($user);
   return $this;
 }

When user 1 wants to add a friend, we could have

 $user1->add_friend($user2);

The problem comes when user 2 wants to remove that person.

 $user2->remove_friend($user1)

The above code would not work for laravel would look up the user_friends table looking for user_id as 2 and friend_id as 1. That does not exist, but the reverse exists, i.e, user_id as 1 and friend_id as 2.

Long story short, belongsToMany would see user 1 as friend with user 2, but not that user 2 is friend with user_1 based on the database table below.

user_id friend_id
1 2
1 3
2 3
2 5

Tried solution

I have tried doing something like the following

/**
* @return App\User[]
*/
public function friends(){
  $query_1 = $this->belongsToMany('App\User', 'user_friend', 'user_id', 'friend_id')->getBaseQuery();
  $query_2 = $this->belongsToMany('App\User', 'user_friend', 'friend_id', 'user_id')->getBaseQuery();

 return $query_1->union($query_2)->get();
}

The problem is that I lose the convenience of function such as add_friend($user) and remove_friend($user) described before.

The query union will generate the following SQL

 (select * from `user` inner join `user_friend` on `user`.`id` = `user_friend`.`friend_id` where `user_friend`.`user_id` = ?) 
union
 (select * from `user` inner join `user_friend` on `user`.`id` = `user_friend`.`user_id` where `user_friend`.`friend_id` = ?)
 union
 (select * from `user` inner join `user_friend` on `user`.`id` = `user_friend`.`user_id` where `user_friend`.`friend_id` = ?)

Suggestion

For select the related models, what about generating a SQL something like the following?

select user.id from `user` where `user`.`user_id` IN 
(
 select * from (
  (select friend_id from user_friend WHERE user_id = 1)
  union
  (select user_id from user_friend WHERE friend_id = 1)
 ) AS subquery -- it is important that this query has a name
)

As eloquent method, something like that:

 public function friends(){
   return $this->belongsToManySymmetrical('App\User', 'user_friends', 'user_id', 'friend_id');
 }

 public function add_friend(User $user) {
   $this->friends()->attach($user); //Would look up for both user_id and friend_id columns to operate
   return $this;
 }

 public function remove_friend(User $user) {
   $this->friends()->detach($user); //Would look up for both user_id and friend_id columns to operate
   return $this;
 }

rulrok avatar Jul 17 '15 16:07 rulrok

:+1:

CMDann avatar Jul 17 '15 16:07 CMDann

This kinda scenarios make me regret using frameworks :) There are so many of them, and they are such brain drainers.

+1

shehi avatar Jul 21 '15 08:07 shehi

+1

jufy avatar Jul 22 '15 08:07 jufy

I think you should not mix meaning of relation. It's a 2 kind of tables(friends_requests, friends_list). Table friends_requests (id, user_from, user_to, approved_at, declined_at, created_at, updated_at, deleted_at) I JUST WANT to be a friend of someone. ->myfriendrequest() //SELECT * FROM friends_requests WHERE user_from = {my.user_id} Someone JUST WANT to be my friends ->wanttobemyfriends() //SELECT * FROM friends_requests WHERE user_to = {my.user_id}

And on Approved_at you must have observer(trigger in DB or ORM observer), when getDirty['approved_at'] == STATUS_APPROVED in saving, insert in friend_list new row, on deleting friend, delete friend_request rows and delete row from friends_list.

Table friends_list(id, user_id, friend_id) ->friends() // SELECT * FROM friends_list WHERE user_id = {my.user_id}

prohalexey avatar Jul 27 '15 13:07 prohalexey

+1 :+1:

CoderFetch avatar Aug 14 '15 12:08 CoderFetch

I dont like this idea,

syaiful6 avatar Sep 26 '15 19:09 syaiful6

+1, had to find ways around to implement this precise situation on my project

daniel-gomes avatar Oct 30 '15 13:10 daniel-gomes

So $user1 calls $user2 a friend, but $user2 does not call $user1 a friend?

It would make sense that $user2->remove_friend($user1) does not do anything in that case, since $user2 cannot change who $user1 calls "friend". This seems more like the "follower" model that Twitter uses.

If the relationship goes both ways, imo that should be reflected in the database:

user_id friend_id
1 2
2 1

codefocus avatar Dec 01 '15 05:12 codefocus

@codefocus If $user2 can or cannot break this relationship will be up to your business logic. In some situations, like mine, whenever one breaks the friendship, this will be broken in both directions.

rulrok avatar Dec 01 '15 10:12 rulrok

Thanks for the report, but we don't process feature requests. We are, however, open to PRs.

GrahamCampbell avatar Dec 19 '15 23:12 GrahamCampbell

Did you found any solution for this scenario? @rulrok

brian2694 avatar Jan 02 '17 23:01 brian2694

@brian2694 Back then, to simplify matters for our project, we had decided that the friendship concept wouldn't exist. Rather, users could follow and be followed by other users.

https://github.com/PanoPla/panopla/blob/develop/app/User.php

I know, it doesn't fix the problem, but it simplifies the project. =p

rulrok avatar Jan 02 '17 23:01 rulrok

I'll analyze it. Thank you.

And I've found this. https://github.com/AbdullahGhanem/friendship

brian2694 avatar Jan 02 '17 23:01 brian2694