1

I've wrote the following Doctrine query:

        $query3 = $this->entityManager
            ->createQueryBuilder()
            ->select('t.textDomain , t.translationKey , t.languageIso , t.translationDate')
            ->from(
                'AMDatabase\Entity\TheVerse\TranslationsMasters',
                't'
            )
            ->groupBy('t.languageIso')
            ->orderBy(
                't.translationDate',
                'DESC'
            );

// the values of $key2 array are:
// en-US
// es-MX
// es-PR

        foreach( $translation AS $key2=>$value2 ) {               

            if ( $key2 == 'en-US' ) {

                $query3
                    ->orWhere(
                        $query3->expr()
                               ->like(
                                   't.languageIso',
                                   ':languageIso'
                               )
                    )
                    ->setParameter(
                        'languageIso',
                        $key2
                    );

            }

        }

        $result3 = $query3->getQuery()
                          ->getArrayResult();

How do I have the query search for all 3 language ISO's at the same time?

  • With "if ( $key2 == 'en-US' ) {" the query executes and gives me the expected result.
  • But if I remove "if ( $key2 == 'en-US' ) {" there are no search results.

I thought by using "orWhere" it would keep adding conditions to the query (where en-US will still produce a match).

Ron Piggott
  • 705
  • 1
  • 8
  • 26
  • Have you tried to call `getSQL()` instead of `getQuery()` to see the generated SQL query ? orWhere seems to be capricious : http://stackoverflow.com/questions/6878448/doctrine-or-where – Ulti Apr 30 '15 at 22:35
  • I've discovered my problem. I used :languageIso. The final bound language ISO was searched for in all instances. A simply modification has resolved this: – Ron Piggott May 01 '15 at 04:01
  • $i = 1; foreach( $translation AS $key2=>$value2 ) { $query3 ->orWhere( $query3->expr() ->like( 't.languageIso', ':languageIso'.$i ) ) ->setParameter( 'languageIso'.$i, $key2 ); ++$i; } – Ron Piggott May 01 '15 at 04:02

1 Answers1

5

In haven't ever been able to get orWhere to function the way I think it should. It might be that you need a Where in your opening definition before you can add an orWhere later. You might have to use the nested orX statement like this instead:

$query3->andWhere($query3->expr()->orX(
    $query3->expr()->like('t.languageIso', $query3->expr()->literal('en-US')),
    $query3->expr()->like('t.languageIso', $query3->expr()->literal('es-MX')),
    $query3->expr()->like('t.languageIso', $query3->expr()->literal('es-PR'))
));

You can't develop the statement through a loop. But, since you've only got three criteria, it's easy enough to write out.

jcropp
  • 1,236
  • 2
  • 10
  • 29