D
D
Deniev2021-09-29 04:05:32
symfony
Deniev, 2021-09-29 04:05:32

How to get non-reciprocal subscriptions?

So I have 2 tables:

User
followers | OneToMany | mappedBy="followed"
following | OneToMany | mappedBy="follower"

Follow
followed | ManyToOne | inversedBy="followers"
follower | ManyToOne | inversedBy="following"

I get the subscribers of the user in this way:

$qb ->join('u.following', 'follow')
    ->where('follow.followed = :user')
    ->setParameter('user', $user)

Subscriptions like this:

$qb ->join('u.followers', 'follow')
    ->where('follow.follower = :user')
    ->setParameter('user', $user)

Now I need the user's subscriptions that are not mutual.
By doing this, I get all the reciprocals:

$qb ->join('u.followers', 'fs')
    ->join('u.following','fg')
    ->where('fs.follower = :user')
    ->andWhere('fg.followed = :user')
    ->setParameter('user', $user)

In my hopes , where('fg.followed != :user') should have just displayed non-reciprocal ones, but in practice nothing is displayed at all. Perhaps this is a stupid mistake, but I have an eternal war with the database, I hope for help.

Thanks,

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2021-09-29
@Minifets

It doesn't work, because you have a request for mutual subscriptions of the curve itself, and you don't understand how join works. The problem you have is in the 2nd join. To paint laziness, so briefly.
As a result of all joins, you get a table in which each follower for the user is compared with following. It is simply impossible to choose non-reciprocal subscriptions from such a pile of data.
For the 2nd requests, you need to join not all of the following, but only those who have followed = fs.follower. Moreover, use left join and then check in where for null.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question