Getting the other side of the relationship in the object of the average person

Sorry if the heading doesn’t make much sense, if you have a suggestion for a more accurate description of my problem, suggest it.

I am working on a Symfony 2 application, and I have User objects that should relate to another user, a simple use case that is friends, but there are more complicated cases. Initially, I had a lot of different relationships between users, and life was great.

Now I need to track metadata by the very relationship between the two users, for example:

  • when the request was requested
  • when he was adopted
  • when it ended.
  • if it is over, there are blocks to prevent the reopening of this relationship.
  • and etc.

I have done quite a bit of research, and it seems that with the Doctrine I cannot have metadata about the relationships themselves, because they are not entities. It has been suggested that I use the average person object, so users have a many-to-many relationship with the Friendship object. The friendship object contains metadata and links to two users.

Now to my problem, if I have this friendship object, how do I get the other side? I have a function that I pass to a user that I know, so I get another user? One of the ways I thought about implementing this is below, but it seems to me that there should be a different way

$user = $this->getCurrentUser(); $friends = array(); foreach($user->getFriends() as $friendship) { $friends[] = $friendship->not($user); // return the user we dont have } 

and $ friendship-> not ():

 public function not($user) { return $this->user1===$user ? $this->user2 : $this->user1; } 
+4
source share
2 answers

I solved the friendship problem a couple of years ago. I don’t remember all the details, but I will try to tell you what I remember.

First of all, look at the section that says:

Real many-to-many associations are less common. [...]

Why are many-to-many associations less common? Because often you want to associate additional attributes with an association, in which case you enter an association class. Consequently, the direct many-to-many association disappears and is replaced by one-many-many-many-one associations between the 3 participating classes.

So, I had User and Friendship objects that were mapped to User and Friendship tables. The latter looked something like this:

 | friendship | +--------------+ | from_id | | to_id | | requested_at | | accepted_at |if this is not null, then it was accepted 

Since I prefer to keep things simple, my User objects did not know about Friendship objects, that is, the Friendship class referred to the User class in unidirectional mode. In this case, this means that you cannot get a list of friends from the User class. Of course, you can implement it in the way that can be done.

Then I had this FriendshipService class ( a service level template that had a method like findBy(User $user) that would query the database, such as "find all friendships where from is equal to $user or to is equal to $user " (Probably, there was something else in this query, but I can’t remember.) After you have a set of friendships from the database, iterating over it and listing all the friends is trivial. You can find the other side of such friendship :

 $otherSide = $friendship->getFrom() == $currentUser ? $friendship->getTo() : $friendship->getFrom(); 
+3
source

If you only keep links, you can maintain a parallel array of users and relationships.

 $user = $this->getCurrentUser(); $friends[] = $user->getFriends(); $friendships[] = $user->getFriendships(); 

Where friends [0] have friendships [0] with $ user.

This is a fairly standard compromise of space for speed and, of course, will require additional steps to insert / remove, etc.

If you store objects, not links, this can take up a lot of space, and it will be better for you to maintain friendships as a completely separate list and look for all friendships containing $ user.

0
source

Source: https://habr.com/ru/post/1386579/


All Articles