5

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!

jarlh
  • 42,561
  • 8
  • 45
  • 63
Juri Bojka
  • 305
  • 2
  • 8
  • 18
  • if you look into the findBy method of Doctrine (and follow it through) you can see that you can set a Criteria() along the way... which is basically what you are trying to do. BUT: don't do it. Create a Repository Class for your Entity and put your own findBy method in there doing what you need. Best practice and faster/easier to implement. – Koalabaerchen Oct 26 '15 at 14:09
  • What do you think about do 3 times findby and in the next step merge arrays ? – Juri Bojka Oct 26 '15 at 15:07

1 Answers1

3

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.

Community
  • 1
  • 1
Tomas Votruba
  • 23,240
  • 9
  • 79
  • 115
  • 1
    Thank you Tomas for your answer. I had hope that i can do this only with findBy :) I have my queryBuilder syntax like : "$this->em->getRepository('myRep')->createQueryBuilder('c')->where('c.x = :var or c.y = :var or c.z = :var ')->setParameter(':var', 100)->getQuery()->getResult();" It also works :) – Juri Bojka Oct 26 '15 at 17:05
  • Yea, it's unfortunate, but the `findBy()` method only covers basic usage. I'm glad you found your solution. – Tomas Votruba Oct 26 '15 at 17:07
  • Thank you Tomas, Greeting for You ! – Juri Bojka Oct 26 '15 at 17:08
  • Thank you to Juri! I've also updated answer with your solution and explanation for `expr()` method. – Tomas Votruba Oct 26 '15 at 17:10
  • While it is good that this answer solves the problem I should point out that it is indeed possible to create a custom criteria object and use findBy. It's just not worth the effort nor is there much documentation on the subject. So the first sentence in the answer is wrong. – Cerad Oct 26 '15 at 19:24
  • @Cerad: Thanks for sharing the knowledge. I'll update the answer. – Tomas Votruba Oct 26 '15 at 19:40