57

Is it possible to use OR statement in Doctrine findBy() method? I know that given array is interpreted as case1 AND case2... Like this

$this->repos['notif']->findBy(array('status' => 1, 'status' => 2, 'status' => 3);

Stands for

SELECT * FROM `notif` WHERE status=1 AND status=2 AND status=3;

Now I need something to stand for:

SELECT * FROM `notif` WHERE status=1 OR status=2 OR status=3;

Is there a way to get all cases?

ArVan
  • 4,225
  • 8
  • 36
  • 58

4 Answers4

185

You can write:

$this->repos['notif']->findBy(array('status' => array(1, 2, 3)));

and that should work too.

user3012985
  • 1,851
  • 2
  • 11
  • 2
23

I know that this is old question. Anyways, it's possible to use Criteria for the complex queries (in Doctrine 2 at least):

$criteria = new \Doctrine\Common\Collections\Criteria();
$criteria
  ->orWhere($criteria->expr()->contains('domains', 'a'))
  ->orWhere($criteria->expr()->contains('domains', 'b'));

$groups = $em
  ->getRepository('Group')
  ->matching($criteria);
gorodezkiy
  • 3,299
  • 2
  • 34
  • 42
  • 3
    For those who downvote, please leave comments to help others understand your decision. Thanks – gorodezkiy Oct 08 '15 at 11:59
  • 1
    How can you add criteria and sort by and limit rules to the repository also? Can you share an example? Right now, the SQL in your example would only add where clauses, how do we add limit & orderby? – Joseph Astrahan Jan 28 '16 at 09:11
  • @JosephAstrahan maybe, unfortunately I have no idea for now and have no project to test it at this moment. But are there any mentions of the limit or orderby in the author's question? – gorodezkiy Jan 28 '16 at 20:48
  • @JosephAstrahan Maybe no longer relevant, but for future reference `Criteria` has an `orderBy` – someonewithpc Jul 21 '20 at 19:16
  • @gorodezkiy this not works because: `Argument 1 passed to Doctrine\ORM\EntityRepository::findBy() must be of the type array, object given` – Serhii Vasko Sep 17 '21 at 21:39
15

As far as I know this is not a supported feature by Doctrine to use IN() queries with findby. You could do two things:

  1. Implement a findByStatus(array $statusTypes) method in your (custom) 'notif' repository class. If you like this approach I can give you a example.

  2. Convert your findBy to the following:

    $qb = $this->repos['notif']->createQueryBuilder('n');
    $data = $qb->where($qb->expr()->in('status', array(1,2,3)))->getQuery()->getResult();
    

That should work either :)

Kees Schepers
  • 2,248
  • 1
  • 20
  • 31
9

If you are using MongoDB and need more complex queries, like "less than" linked together with OR, but cannot use a query builder, this also works with this syntax:

   ->findBy(array(
                '$or' => array(
                    array('foo' => array('$lt' => 1234)),
                    array('$and' => array(
                        array('bar' => 45678),
                        array('baz' => array('$lt' => 89013))
                    ))
                )
    ));

Or as a solution for your question:

   ->findBy(array(
                '$or' => array(
                    array('status' => 1),
                    array('status' => 2),
                    array('status' => 3),
                )
    ));
Paul Weber
  • 6,518
  • 3
  • 43
  • 52
  • 2
    This does not work in the case outlined in the question. Isn't this mongodb specific syntax? – karka91 Sep 02 '15 at 08:05
  • 2
    I haven't used MongoDB with doctrine, but that is definitely MongoDB syntax. – Scott Flack Nov 16 '15 at 02:49
  • You are right, but this is passed through. I use this code in production with Doctrine and Symphony and it works (seems to be passed through) and returns correct results. This is my stack ... "doctrine/mongodb-odm-bundle": "3.0.*@dev", "doctrine/mongodb-odm": "dev-master", "doctrine/mongodb": "dev-master", "ext-mongo": "*", – Paul Weber Nov 16 '15 at 09:44
  • 1
    I can also say that this does not work with non mongoDB setups. – Joseph Astrahan Jan 28 '16 at 09:10
  • Whoopsie. Yes! I thought the question was mongodb specific. Will note this in my answer! – Paul Weber Jan 29 '16 at 09:25