Zend framework Zend框架复合体Where语句

Zend framework Zend框架复合体Where语句,zend-framework,zend-db,Zend Framework,Zend Db,此方法作为官方示例发布 ->其中(“价格$maximumPrice”) 这种方法安全吗 想把它写成 ->其中(“价格”,$minimumPrice,$maximumPrice) 有可能吗 我无法将其拆分为两个where语句,因为我计划编写查询 ->其中(“1或2”) ->其中(“3或4”)试试这个: $query->where('(price < ?', $minPrice) ->orWhere('price > ?)', $maxPrice) ->where('s

此方法作为官方示例发布

->其中(“价格<$minimumPrice或价格>$maximumPrice”) 这种方法安全吗

想把它写成 ->其中(“价格<或价格>”,$minimumPrice,$maximumPrice) 有可能吗

我无法将其拆分为两个where语句,因为我计划编写查询 ->其中(“1或2”) ->其中(“3或4”)

试试这个:

$query->where('(price < ?', $minPrice)
->orWhere('price > ?)', $maxPrice)
->where('some = ?', $some_other_variable);
$query->where('(价格<?',$minPrice)
->或其中('price>?),$maxPrice)
->其中('some=?',$some_其他_变量);
将导致:
其中((price<$minPrice)或(price>$maxPrice))和(some=$some\u其他变量)


请注意,如果我有复杂的
WHERE
子句,则使用db adapters的
->quoteInto()
方法,例如:

$where = '('
           . $dbAdapter->quoteInto('price1 < ?', $price1)
           . ' OR '
           . $dbAdapter->quoteInto('price1 > ?', $price1)
       . ')'
       . ' AND '
       . '('
           . $dbAdapter->quoteInto('price2 < ?', $price2)
           . ' OR '
           . $dbAdapter->quoteInto('price2 > ?', $price2)
       . ')'
       ;

$select->where($where);
$where='('
.$dbAdapter->quoteInto($price1<?',$price1)
“或者”
.$dbAdapter->quoteInto('price1>?',$price1)
. ')'
. ' 而且
. '('
.$dbAdapter->quoteInto($price2<?',$price2)
“或者”
.$dbAdapter->quoteInto('price2>?',$price2)
. ')'
;
$select->where($where);

有些时候,您可能希望进行SQL查询,这些查询中的多个where条件都用括号括起来,可以很容易地用foreach解析,但您不想为字符串操作而烦恼。例如,如果您有一个id为且必须为特定类型的用户列表,您可以尝试以下操作:

$select = $this->select();
$subWhere = $this->select();
foreach(array_keys($idArr) as $key => $value) {
  $subWhere->orWhere('id=?', $value);
}
$select->where(implode(' ', $subWhere->getPart('WHERE')))->where('type=?', 'customer');
这将导致“从表中选择*,其中((id=X)或(id=Y)或(id=Z)…)和(type='customer');”

这个想法进一步发展,您可以扩展Zend_Db_Table_Abstract:

public function subWhere($col, $binds, $operands, $andOr = 'OR' )
{
    $subWhere = $this->select();
    if(strtolower($andOr) == 'or') {
        foreach($binds as $key => $value) {
            $subWhere->orWhere($col.$operands[$key].'?', $value);
        }
        return implode(' ', $subWhere->getPart('WHERE'));
    }
    elseif (strtolower($andOr) == 'and') {
        foreach ($binds as $key => $value) {
            $subWhere->where($col.$operands[$key].'?', $value);
        }
        return implode(' ', $subWhere->getPart('WHERE'));
    }
    else {
        return false;
    }
}
并将其用作:

$this->select()->where($this->subWhere($col, $binds, $operands));
当然,您应该允许混合$cols,$operans=array()默认为“=?”等,但为了简单起见,我忽略了这一点。但是我认为我们应该使用本机SQL函数,比如IN(),在。。。和…之间,而不是。。。还有。。。?Zend Framework并没有让您的生活变得非常轻松。

$select->where($db->quoteInto('field1<?OR',$minPrice)
$select->where($db->quoteInto('field1 < ? OR', $minPrice)
             . $db->quoteInto('field1 > ?', $maxPrice))
       ->where($db->quoteInto('field2 < ? OR', $value2)
             . $db->quoteInto('field2 > ?', $value3));
.db->quoteInto('field1>?',$maxPrice)) ->其中($db->quoteInto($field2<?OR',$value2) .$db->quoteInto('field2>?',$value3));
这不合适-我需要像这样编写构造->where(->where(1)或->where(2))->where(->where(3)或->where(4)),我以前问过同样的问题。看看Jason的答案:这是一个有效的变体,但不太好:)