subquery in join with doctrine dql

Posted by Martijn de Munnik on Stack Overflow See other posts from Stack Overflow or by Martijn de Munnik
Published on 2012-10-31T16:59:17Z Indexed on 2012/10/31 17:00 UTC
Read the original article Hit count: 246

Filed under:
|
|
|

I want to use DQL to create a query which looks like this in SQL:

select
    e.*
from
    e
inner join (
    select
        uuid, max(locale) as locale
    from
        e
    where
        locale = 'nl_NL' or
        locale = 'nl'
    group by
        uuid
) as e_ on e.uuid = e_.uuid and e.locale = e_.locale

I tried to use QueryBuilder to generate the query and subquery. I think they do the right thing by them selves but I can't combine them in the join statement. Does anybody now if this is possible with DQL? I can't use native SQL because I want to return real objects and I don't know for which object this query is run (I only know the base class which have the uuid and locale property).

    $subQueryBuilder = $this->_em->createQueryBuilder();
    $subQueryBuilder
        ->addSelect('e.uuid, max(e.locale) as locale')
        ->from($this->_entityName, 'e')
        ->where($subQueryBuilder->expr()->in('e.locale', $localeCriteria))
        ->groupBy('e.uuid');

    $queryBuilder = $this->_em->createQueryBuilder();
    $queryBuilder
        ->addSelect('e')
        ->from($this->_entityName, 'e')
        ->join('('.$subQueryBuilder.') as', 'e_')->join
        ->where('e.uuid = e_.uuid')
        ->andWhere('e.locale = e_.locale');

© Stack Overflow or respective owner

Related posts about doctrine2

Related posts about subquery