$maximu开发者_运维技巧mPrice\")" />
开发者

Zend Framework Complex Where Statement

This method is published as offical example

->where("price < $minimumPrice OR price > $maximu开发者_运维技巧mPrice") is such method safe?

want to write it as ->where("price < ? OR price > ?", $minimumPrice, $maximumPrice) are there any poissibility?

and I can't split it into 2 where statements because plan to write query ->where("1 OR 2") ->where("3 OR 4")


Try this:

$query->where('(price < ?', $minPrice)
->orWhere('price > ?)', $maxPrice)
->where('some = ?', $some_other_variable);

will result: where ((price < $minPrice) OR (price > $maxPrice)) AND (some = $some_other_variable)

Note the double (( )) in OR part


If I have complex WHERE clauses I use the db adapters' ->quoteInto() method like:

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

$select->where($where);


Some times you will want to make SQL queries which have parenthesis around multiple where conditions that would be easily parsed with foreach, but you do not want to be bothered about string manipulation. For example, you would have a list of users with id's and that have to be of certain type, you can try this:

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

This will result in "SELECT * FROM table WHERE ((id=X) OR (id=Y) OR (id=Z)...) AND (type='customer');"

The idea developed a bit further, you could extend the 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;
    }
}

And use it as:

$this->select()->where($this->subWhere($col, $binds, $operands));

Of course you should allow mixed $cols, $operands = array() defaulting as '=?' etc. but for the sake of simplicity I left that out. But I believe we should use native SQL functions like IN(), BETWEEN ... AND ..., NOT BETWEEN ... AND ...? Zend Framework doesn't make your life very easy though.


$select->where($db->quoteInto('field1 < ? OR', $minPrice)
             . $db->quoteInto('field1 > ?', $maxPrice))
       ->where($db->quoteInto('field2 < ? OR', $value2)
             . $db->quoteInto('field2 > ?', $value3));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜