Skip to content

Instantly share code, notes, and snippets.

@roukmoute
Last active July 27, 2023 15:07
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save roukmoute/d8b7473da78fc9d8b867 to your computer and use it in GitHub Desktop.
Save roukmoute/d8b7473da78fc9d8b867 to your computer and use it in GitHub Desktop.
Concrete example of WHERE...IN subquery in doctrine 2
<?php
class MyRepository extends EntityRepository
{
public function whereInSubQuery(User $user)
{
$queryBuilder = $this->createQueryBuilder('my_repository');
$queryBuilder
->where(
$queryBuilder->expr()->in(
'my_repository.skill',
$this
->createQueryBuilder('subquery_repository')
->select('skill.id')
->from('EntityBundle:Skill', 'skill')
->where('skill.user = :user')
->getDQL()
)
)
->setParameter(':user', $user);
return $queryBuilder->getQuery()->getResult();
}
}
@achilleskineur
Copy link

Hello many thank for this gist but there is a issue with

  $this->createQueryBuilder('subquery_repository')

This function in repository return

      $this->_em->createQueryBuilder()
            ->select($alias)
            ->from($this->_entityName, $alias);

You have to replace with

$this->_em->createQueryBuilder()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment