带有 OR 条件的原则 findBy

发布于 2025-01-04 15:22:06 字数 472 浏览 5 评论 0原文

是否可以在 Doctrine findBy() 方法中使用 OR 语句? 我知道给定的数组被解释为 case1 AND case2... 就像

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

现在代表

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

我需要一些东西来代表:

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

有没有办法获得所有案例?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

格子衫的從容 2025-01-11 15:22:06

你可以写:

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

这也应该有效。

You can write:

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

and that should work too.

情泪▽动烟 2025-01-11 15:22:06

我知道这是老问题了。无论如何,可以使用 Criteria 进行复杂查询(至少在原则 2 中):

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

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);
戒ㄋ 2025-01-11 15:22:06

据我所知,Doctrine 不支持将 IN() 查询与 findby 一起使用的功能。您可以做两件事:

  1. 在(自定义)“notif”存储库类中实现 findByStatus(array $statusTypes) 方法。如果您喜欢这种方法,我可以给您举一个例子。

  2. 将您的 findBy 转换为以下内容:

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

这应该可以工作:)

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

情独悲 2025-01-11 15:22:06

如果您正在使用 MongoDB 并且需要更复杂的查询,例如与 OR 链接在一起的“小于”,但无法使用查询生成器,这也适用于以下语法:

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

或者作为您问题的解决方案:

   ->findBy(array(
                '$or' => array(
                    array('status' => 1),
                    array('status' => 2),
                    array('status' => 3),
                )
    ));

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),
                )
    ));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文