Ron Piggott
Ron Piggott

Reputation: 705

ZF2 Doctrine Query With Multiple OR Conditions

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?

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

Upvotes: 1

Views: 1412

Answers (1)

jcropp
jcropp

Reputation: 1246

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.

Upvotes: 5

Related Questions