【问题标题】:Multiple conditions on same column with propel queries具有推进查询的同一列上的多个条件
【发布时间】:2015-03-19 20:10:47
【问题描述】:

我正在使用 symfony2.5 和 propel 1.5。

假设我的实体名为FooFoo 有一个属性numericalValue。我想查询数据库以仅显示 Foos 的 numericalValue 介于 5 和 10 或 15 和 20 之间。

SQL 语句如下所示:

SELECT *
FROM foo
WHERE
    (numericalValue >= 5 AND numericalValue <= 10)
    OR
    (numericalValue >= 15 AND numericalValue <=  20)

我想用一个推进查询来表示它。

我试过了:

$query = FooQuery::create('Foo');
$query->condition("min", "Foo.numericalValue >= :bar1", 5);
$query->condition("max", "Foo.numericalValue <= :bar2", 10);
$query->combine(array("min","max"), "and", "first");

$query->condition("min", "Foo.numericalValue >= :bar3", 15);
$query->condition("max", "Foo.numericalValue <= :bar4", 20);
$query->combine(array("min","max"), "and", "second");


$query->combine(array("first","second"), "OR");

由于参数的数量与参数的数量不匹配而导致异常返回。

$query-&gt;toString():

Criteria: SQL (may not be complete):
SELECT FROM `Foo` WHERE 
(    
  (Foo.numericalValue >= :bar1 AND Foo.numericalValue <= :bar2) OR
  (Foo.numericalValue >= :bar3 AND Foo.numericalValue <= :bar4)
)
Params: 
  Foo.numericalValue => 5,
  Foo.numericalValue => 10,
  Foo.numericalValue => 15,
  Foo.numericalValue => 20

我希望Params: bar1 =&gt; 5, bar2 =&gt; 10, bar3 =&gt; 15, bar4 =&gt; 20

我该如何解决这个问题?

【问题讨论】:

    标签: symfony propel


    【解决方案1】:

    Params: 下列出的参数不反映将要绑定的值。

    删除die($query-&gt;toString()); 后它实际上对我有用。我有一段时间没有尝试过,因为列出的参数仍然不符合我的期望。但似乎参数数量与参数数量不匹配的异常在某个时候消失了。

    简而言之:问题中的代码有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多