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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With