Doctrine findBy with OR condition

白昼怎懂夜的黑 提交于 2019-12-03 00:56:08

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 :)

You can write:

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

and that should work too.

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);

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),
                )
    ));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!