Here's how I try:
$qb->select(array('DISTINCT i.id', 'i.name', 'o.name')) ->from('Item', 'i') ->join('i.order', 'o') ->where( $qb->expr()->in( 'o.id', $qb2->select('o2.id') ->from('Order', 'o2') ->join('Item', 'i2', \Doctrine\ORM\Query\Expr\Join::WITH, $qb2->expr()->andX( $qb2->expr()->eq('i2.order', 'o2'), $qb2->expr()->eq('i2.id', '?1') ) ) ->getDQL() ) ) ->andWhere($qb->expr()->neq('i.id', '?2')) ->orderBy('o.orderdate', 'DESC') ->setParameter(1, 5) ->setParameter(2, 5) ;
I did not check this, of course, and made some assumptions about your models. Possible problems:
- Limit: this was some problem in Doctrine 2, it seems that the query builder is not very good at accepting restrictions. Look here , here and here .
- The IN clause is usually used with an array, but I think it will work with a subquery.
- You can probably use the same parameter? 1 instead of two parameters (because they are of the same value), but I'm not sure.
In conclusion, this may not work the first time, but it will certainly lead you to the right path. After that, tell us the final 100% correct answer.
faken Jul 09 '11 at 23:22 2011-07-09 23:22
source share