如何在教义中使用、在何处、在何处?

时间:2021-11-18 20:06:38
WHERE a = 1 AND (b = 1 Or b = 2) AND (c = 1 OR c = 2)

How can i make this in Doctrine?

我怎么能在教义中这么说呢?

$q->where("a = 1");
$q->andWhere("b = 1")
$q->orWhere("b = 2")
$q->andWhere("c = 1")
$q->orWhere("d = 2")

this isnt correctly... Should be:

这不是正确的…应该是:

$q->where("a = 1");
$q->andWhere("b = 1")
   $q->orWhere("b = 2")
$q->andWhere("c = 1")
   $q->orWhere("d = 2")

but how can i make it? In Propel is function getNewCriterion, and in Doctrine...?

但是我怎么做呢?在推进中是函数获取新准则,而在学说中…?

4 个解决方案

#1


84  

$q->where("a = 1")
  ->andWhere("b = 1 OR b = 2")
  ->andWhere("c = 2 OR c = 2")
  ;

#2


44  

Here's an example for those who have more complicated conditions and using Doctrine 2.* with QueryBuilder:

这里有一个例子,对于那些有更复杂的条件和使用教条2的人。QueryBuilder *:

$qb->where('o.foo = 1')
   ->andWhere($qb->expr()->orX(
      $qb->expr()->eq('o.bar', 1),
      $qb->expr()->eq('o.bar', 2)
   ))
  ;

Those are expressions mentioned in Czechnology answer.

这些是捷克技术回答中提到的表达。

#3


11  

Why not just

为什么不直接

$q->where("a = 1");
$q->andWhere("b = 1 OR b = 2");
$q->andWhere("c = 1 OR d = 2");

EDIT: You can also use the Expr class (Doctrine2).

编辑:您也可以使用Expr类(Doctrine2)。

#4


1  

One thing missing here: if you have a varying number of elements that you want to put together to something like

这里少了一件事:如果你想把不同数量的元素放在一起,比如

WHERE [...] AND (field LIKE '%abc%' OR field LIKE '%def%')

and dont want to assemble a DQL-String yourself, you can use the orX mentioned above like this:

不要自己组装一个DQL-String,你可以使用上面提到的orX:

$patterns = ['abc', 'def'];
$orStatements = $qb->expr()->orX();
foreach ($patterns as $pattern) {
    $orStatements->add(
        $qb->expr()->like('field', $qb->expr()->literal('%' . $pattern . '%'))
    );
}
$qb->andWhere($orStatements);

#1


84  

$q->where("a = 1")
  ->andWhere("b = 1 OR b = 2")
  ->andWhere("c = 2 OR c = 2")
  ;

#2


44  

Here's an example for those who have more complicated conditions and using Doctrine 2.* with QueryBuilder:

这里有一个例子,对于那些有更复杂的条件和使用教条2的人。QueryBuilder *:

$qb->where('o.foo = 1')
   ->andWhere($qb->expr()->orX(
      $qb->expr()->eq('o.bar', 1),
      $qb->expr()->eq('o.bar', 2)
   ))
  ;

Those are expressions mentioned in Czechnology answer.

这些是捷克技术回答中提到的表达。

#3


11  

Why not just

为什么不直接

$q->where("a = 1");
$q->andWhere("b = 1 OR b = 2");
$q->andWhere("c = 1 OR d = 2");

EDIT: You can also use the Expr class (Doctrine2).

编辑:您也可以使用Expr类(Doctrine2)。

#4


1  

One thing missing here: if you have a varying number of elements that you want to put together to something like

这里少了一件事:如果你想把不同数量的元素放在一起,比如

WHERE [...] AND (field LIKE '%abc%' OR field LIKE '%def%')

and dont want to assemble a DQL-String yourself, you can use the orX mentioned above like this:

不要自己组装一个DQL-String,你可以使用上面提到的orX:

$patterns = ['abc', 'def'];
$orStatements = $qb->expr()->orX();
foreach ($patterns as $pattern) {
    $orStatements->add(
        $qb->expr()->like('field', $qb->expr()->literal('%' . $pattern . '%'))
    );
}
$qb->andWhere($orStatements);