Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine findBy or statement

I have problem with my doctrine findBy method. I know how to do in doctrine

SELECT * FROM Y WHERE Z = 1 OR Z = 2 OR Z = 3

But I don't know how to do

SELECT * FROM Y WHERE Z = 10 OR Y = 10 OR X = 10

I must add that I don't want to do this in querybuilder, only in this way: findBy(array(1=> "aaa")). Is it possible?

Greetings!

like image 692
Juri Bojka Avatar asked Oct 18 '22 23:10

Juri Bojka


1 Answers

You can find your answer here.

To be specific about your use case:

$queryBuilder = $em->getRepository('YourEntityClassName')
    ->createQueryBuilder('c');

$result = $queryBuilder->select('c')
    ->where($queryBuilder->expr()->orX(
        $queryBuilder->expr()->eq('c.x', ':value'),
        $queryBuilder->expr()->eq('c.y', ':value'),
        $queryBuilder->expr()->eq('c.z', ':value')
    ))
    ->setParameter('value', 10)
    ->getQuery()
    ->getResult();

Or simple:

$queryBuilder = $em->getRepository('YourEntityClassName')
    ->createQueryBuilder('c');

$result = $queryBuilder->select('c')
    ->where('c.x = :value or c.y = :value or c.z = :value')
    ->setParameter(':value', 10)
    ->getQuery()
    ->getResult()

$queryBuilder->expr() is here only to prevent typos and make your code more futureproof, since it's wrapped in methods.


It's possible with findBy() via Criteria, yet difficult and not well documented, as pointed out by @Cedar.

like image 68
Tomas Votruba Avatar answered Oct 27 '22 10:10

Tomas Votruba