SELECT c.*, cr.rate_date, cr.rate FROM currency c LEFT JOIN currency_rate cr ON cr.id = (SELECT MIN(cr2.id) FROM currency_rate cr2 WHERE cr2.currency_id = c.id)
Niestety nie umiem poradzić sobię z subQuery. Mam coś takiego:
$subQuery = $em->getRepository('AppBundle:CurrencyRate')->createQueryBuilder('cr2') ->select('MIN(cr2.id)') ->where('cr2.currency_id = c0_.id'); $currencies = $em->getRepository('AppBundle:Currency')->createQueryBuilder('c') ->leftJoin('AppBundle\Entity\CurrencyRate', 'cr', 'with', 'cr.id = :subQuery')->addSelect('cr.rateDate, cr.rate') ->setParameter(':subQuery', $subQuery->getDQL()) ->getQuery()->getResult();
ale zapytanie które się tworzy to:
SELECT c0_.id AS id_0, c0_.code AS code_1, c0_.active AS active_2, c0_.main AS main_3, c1_.rate_date AS rate_date_4, c1_.rate AS rate_5 FROM currency c0_ LEFT JOIN currency_rate c1_ ON (c1_.id = 'SELECT MIN(cr2.id) FROM AppBundle\\Entity\\CurrencyRate cr2 WHERE cr2.currency_id = c0_.id');
Czyli całe to subQuery jest traktowane jako string. Szukałem odpowiedzi w Google ale żadne rozwiązanie nie zadziałało :/