A
A
Alexander Evgenievich2015-01-28 13:34:15
MySQL
Alexander Evgenievich, 2015-01-28 13:34:15

How to properly organize many-to-many relationship in Symfony2 Doctrine2?

Hello.
There are two tables (tables for example, but they reflect the essence of the problem):
--------------------
Users:
- id
- name
---------- ----------
Followers:
- id
- user_id
- follower_id
--------------------
I need to link these tables in such a way that for a specific user it was possible to get his subscribers, and for the subscriber, it would be possible to get everyone to whom he is subscribed. Accordingly, I considered the connection Many-To-Many, Self-referencing. In the example, links are established in the user table, which means that with each user request, it will pull both its subscribers and those to whom it is subscribed (in my case). I need relationships to be set to the "Followers" table so that when queried by user:

$repository->findOneBy(array('user' => $this->getUser()));

The following data was given:
- id
- user (User object)
- followers (Array of user objects)
Is it possible to do this? If possible, how to write ManyToMany annotations?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
keltanas, 2015-01-28
@banderos120

You don't need ManyToMany between User and Follower, because Follower is a user anyway. Those. you need a ManyToMany relationship between Users and Users :) You did the same in the example with 2 tables. But, this option will not be very flexible.
It would be better to make a separate Followship entity in which to establish 2 connections to the User of the OneToMany type. But, only if you need to store some other properties for Followship, for example, createdAt.
Actually, the example that you found is quite fair and ready to use.
To get who is following a user, refer to the $friendsWithMe property.
To get who a user is following, see $myFriends.
What information does the signing identifier carry in your example?
What does "pull" mean? Why did you decide so? It depends how you establish the greed for communication.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question