2013-04-12 83 views
0

我到目前爲止能夠在zend2 SELECT查詢中應用AND和LIKE,但無法弄清楚如何應用OR運算符。這是我的查詢:如何在zend2選擇查詢中應用OR運算符

$this->table = $data['table']; 
    $select = new Select(); 
    $spec = function (Where $where) { 
     $where->like('title', 'border%'); 
    }; 
    $select->from($this->table) 
      ->where(array('id'=> 8)); 
    $select->where($spec); 
    $resultSet = $this->selectWith($select); 
    $resultSet->buffer(); 
    return $resultSet; 

它返回這條SQL語句

SELECT `rs_morning_report`.* 
FROM `rs_morning_report` 
WHERE `id` = :where1 AND `title` LIKE :where2 

現在我想添加 「AND(部門LIKE%A%或部門LIKE%B%)」

回答

3

你可以使用Where對象鏈接它們。

$where = new Where(); 
$where->equalTo('fieldOne', 'XX') 
    ->AND 
    ->equalTo('field_two', 'XXX') 
    ->OR 
    ->equalTo('field_three', 'XXX') 
; 

$select->where($where); 

您也可以嵌套他們,如果你需要:

$where = new Where(); 
$where->equalTo('fieldOne', 'XX') 
    ->NEST 
     ->equalTo('field_two', 'XXX') 
     ->OR 
     ->equalTo('field_three', 'XXX') 
    ->UNNEST 
; 

$select->where($where); 
0

要生成OR語句,您可以使用Zend\Db\Sql\Predicate\PredicateSet與參數PredicateSet::COMBINED_BY_OR

實施例:

use Zend\Db\Sql\Predicate; 

$select->where(array(
    // Other conditions... 
    new Predicate\PredicateSet(
     array(
      new Predicate\Like('sector', '%'.$sectorA.'%'), 
      new Predicate\Like('sector', '%'.$sectorB.'%'), 
     ), 
     Predicate\PredicateSet::COMBINED_BY_OR 
    ), 
));