Answer the question
In order to leave comments, you need to log in
Checking for the existence of a many-to-many relationship in Doctrine?
I have an Article class:
/**<br>
* @ORM\ManyToMany(targetEntity="Acme\UserBundle\Entity\User", inversedBy="like_articles")<br>
*/<br>
protected $like_users;<br>
/**<br>
* @ORM\ManyToMany(targetEntity="Acme\BlogBundle\Entity\Article", mappedBy="like_users")<br>
*/<br>
protected $like_articles;<br>
$article = $em->find('AcmeBlogBundle:Article', $article_id);<br><br>
if($article->getLikeUsers()->contains($this->getUser()))<br>
{<br>
throw new BadRequestHttpException('Relationship exist');<br>
}<br><br>
SELECT <br>
t0.username AS username1,<br>
...<br>
FROM<br>
user t0 <br>
INNER JOIN article_user <br>
ON t0.id = article_user.user_id <br>
WHERE article_user.article_id = '6252';<br>
AND t0.id = <id пользователя><br>
Answer the question
In order to leave comments, you need to log in
It would be logical if the request also included:
Because you check with the user later in PHP, and not in the request 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();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question