Checking existence of connection many-to-many in Doctrine

I have class Article:
/**
 * @ORM\ManyToMany(targetEntity="Acme\UserBundle\Entity\User", inversedBy="like_articles")
*/
protected $like_users;


the User class:
/**
 * @ORM\ManyToMany(targetEntity="Acme\BlogBundle\Entity\Article", mappedBy="like_users")
*/
protected $like_articles;


The established connection indicates that the user has marked an article as any.

And of course the user can mark an article once to check it out do so:
$article = $em->find('AcmeBlogBundle:Article', $article_id);

if($article->getLikeUsers()->contains($this->getUser()))
{
 throw new BadRequestHttpException('Relationship ' exist');
}



Include query log in mysql and see that to check the contains generated the following query:
SELECT 
 t0.username AS username1,
...
FROM
 user t0 
 INNER JOIN article_user 
 ON t0.id = article_user.user_id 
WHERE article_user.article_id = '6252';


It would be logical if the query was another:
AND t0.id = <user id>
</id>


Thus, if the article will vote 10,000 people, to test you voted 10001, the user will be retrieved 10000 records?

I'm new to Symfony, tell me, what's wrong?
October 3rd 19 at 03:37
2 answers
October 3rd 19 at 03:39
It would be logical if the query was another:

It is not logical. join covers this condition with the head
how did he cover that? — query selects all users who voted for an article 6252 - manue commented on October 3rd 19 at 03:42
ON t0.id = article_user.user_id — this means that each user_id for a matching t0.id - buck commented on October 3rd 19 at 03:45
ok — 10,000 users voted for one article on each of the 10,000 user_id will be 1 record as a result get 10,000 entries - manue commented on October 3rd 19 at 03:48
October 3rd 19 at 03:41
Because You are reconciled with the user later in the script, not the query itself. Should be something like this:

$count = $this->entityManager->getRepository('AcmeBlogBundle:Article')->createQueryBuilder('a')
->select('COUNT(a)')
 ->join('a.like_users', 'u')
 ->where('a.id = :article_id')
 ->setParameter("article_id", $article_id)
 ->andWhere('u.id = :user_id')
 ->setParameter("user_id", $this->getUser()->getId())
->getQuery()
->getSingleScalarResult();
it is clear, through the orm, it turns out you can't do it - manue commented on October 3rd 19 at 03:44

Find more questions by tags Doctrine ORMSymfonyMany-to-many