Skip to content

Instantly share code, notes, and snippets.

@achilleskineur
Forked from roukmoute/EntityRepository.php
Last active July 11, 2018 13:29
Show Gist options
  • Save achilleskineur/0caf32f38cffc6ed979c8b77d762cfba to your computer and use it in GitHub Desktop.
Save achilleskineur/0caf32f38cffc6ed979c8b77d762cfba 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
->_em->createQueryBuilder('subquery_repository')
->select('skill.id')
->from('EntityBundle:Skill', 'skill')
->where('skill.user = :user')
->getDQL()
)
)
->setParameter(':user', $user);
return $queryBuilder->getQuery()->getResult();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment