1

I've tried to find how to use column value as array key when getting results using queryBuilder. I found this question Using column value as array index in doctrine but unfortunately it doesn't work in my case as I'm using left join.

Currently I have this query:

        $qb = $entityManager->createQueryBuilder('translation');
    $qb->select('translation')
        ->from('MainBundle:PageTranslation', 'translation', 'translation.id')
        ->leftJoin('MainBundle:Page', 'page', 'WITH', 'IDENTITY(translation.page) = page.id')
        ->where('translation.locale = :locale')
        ->andWhere('translation.enabled = :enabled')
        ->andWhere('page.category = :category')
        ->setParameter('category', $category)
        ->setParameter('locale', $locale)
        ->setParameter('enabled', true);
    $result = $qb->getQuery()->getResult();

Results I get has array keys starting with 0 and incrementing by one. I need array keys to be Page IDs (page.id) or IDENTITY of translation.page

3
  • Does each page have only one translation? I remember trying something like this with 'INDEX BY' with a oneToMany association and it didn't work out well, i.e: you won't get a nested array as a mapping from page to translations, but each page id will only have one translation assigned to it. Aug 12, 2015 at 14:04
  • No each page may have multiple translations
    – Einius
    Aug 12, 2015 at 14:08
  • Then I suggest you do it manually. Aug 12, 2015 at 15:08

1 Answer 1

0

If you look at the QueryBuilder API you'll see that the innerJoin() method has $indexBy as it's last argument. Pass 'translation.id' or ( the pk of translation instead of 'id') as the last argument, to get the desired result.

1
  • If I add indexBy argument I get error [Syntax Error] line 0, col 100: Error: Expected Literal, got 'BY'
    – Einius
    Aug 12, 2015 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.