2016-01-28 94 views
0

我想添加括號我的請求,但我不知道如何。你可以幫我嗎 ?Zend 2:謂詞和'('&')'

我的SQL查詢:

SELECT * 
FROM vw_my_asn_header 
WHERE username = 'toto' 
    AND (shipment_number LIKE '20151106052811' OR 
     shipment_number LIKE '20151110053250' OR 
     shipment_number LIKE '20151116054359') 
ORDER BY message_id ASC 

我的Zend查詢:

public function searchSitesDeliveries($username, Search $search) 
{ 
    $select = $this->tableGateway->getSql()->select(); 
    array(new Predicate\Expression('username = ?', $username)),Predicate\PredicateSet::COMBINED_BY_AND); 

    if (!empty($search->get_shipment_number())) 
    { 
     $valeur = $search->get_shipment_number(); 

     if(is_array($valeur)) { 
      $valeur = array_unique($valeur); 

      foreach ($valeur as $key => $value) { 
       $predicate_set->orPredicate(new Predicate\Like('shipment_number', '%'.$value.'%')); 
      } 
     } 
     else { 
      $predicate_set->andPredicate(new Predicate\Like('shipment_number', '%'.$valeur.'%')); 
     } 
    } 

    $select->where($predicate_set); 
    $resultSet = $this->tableGateway->selectWith($select); 

    return $resultSet; 
} 

它缺乏用戶名和類人猿和where子句的結束之間的括號。

["queryString"] => string(181) "SELECT vw_my_asn_header FROM vw_my_asn_header WHERE (username = :where1 AND shipment_number LIKE :where2 OR shipment_number LIKE :where3 OR shipment_number LIKE :where4)" 

如何添加它們?

回答

0

嘗試使用方法巢()和UNNEST()周圍的循環:

 $predicate_set->nest(); 
     foreach ($valeur as $key => $value) { 
      $predicate_set->orPredicate(new Predicate\Like('shipment_number', '%'.$value.'%')); 
     } 
     $predicate_set->unnest();