Zend Framework Complex Where Statement

前端 未结 4 1393
感动是毒
感动是毒 2021-01-07 03:04

This method is published as offical example

->where(\"price < $minimumPrice OR price > $maximumPrice\") is such method safe?

want to write it as ->where

相关标签:
4条回答
  • 2021-01-07 03:24

    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.

    0 讨论(0)
  • 2021-01-07 03:26

    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

    0 讨论(0)
  • 2021-01-07 03:41

    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);
    
    0 讨论(0)
  • 2021-01-07 03:43
    $select->where($db->quoteInto('field1 < ? OR', $minPrice)
                 . $db->quoteInto('field1 > ?', $maxPrice))
           ->where($db->quoteInto('field2 < ? OR', $value2)
                 . $db->quoteInto('field2 > ?', $value3));
    
    0 讨论(0)
提交回复
热议问题