2014-02-07 71 views
1

我想我的查詢是這樣的:查詢與Zend Framework中括號2.2

SELECT tbl_bids. * , tbl_department.vDeptName, tbl_user.vFirst 
FROM tbl_bids 
LEFT JOIN tbl_bids_department ON tbl_bids_department.iBidID = tbl_bids.iBidID 
LEFT JOIN tbl_department ON tbl_department.iDepartmentID = tbl_bids_department.iDepartmentID 
LEFT JOIN tbl_user ON tbl_user.iUserID = tbl_bids.iUserID 
WHERE tbl_user.iUserID = '1' // with parantheses in where clause 
AND (
tbl_department.vDeptName = 'PHP' 
OR tbl_department.vDeptName = 'android' 
) 
GROUP BY tbl_bids.iBidID 
ORDER BY iBidID DESC 
LIMIT 0 , 30 

但我不能找到讓括號在我的查詢方式,還有多髮狀況和環路將在那裏進行where子句.. 這裏是我的代碼

$select = $this->tableGateway->getSql()->select(); 
    $select->columns(array('*')) 
      ->join('tbl_bids_department', 'tbl_bids_department.iBidID = tbl_bids.iBidID', array(),"LEFT") 
      ->join('tbl_department', 'tbl_department.iDepartmentID = tbl_bids_department.iDepartmentID',array(tbl_department.vDeptName),"LEFT") 
      ->join('tbl_user', 'tbl_user.iUserID = tbl_bids.iUserID',array(tbl_user),"LEFT") 
      ->group('tbl_bids.iBidID'); 

    $where = new \Zend\Db\Sql\Where(); 
    $where->equalTo('tbl_bids.eDeleted', '0'); 

    $sWhere = new \Zend\Db\Sql\Where(); 
    for ($i=0 ; $i<count($aColumns) ; $i++) 
    { 
     if (isset($data['sSearch_'.$i]) && $data['sSearch_'.$i] != "") 
     { 
      if($aColumns[$i] == 'vDeptName'){ 
       $allDept = explode(',', $data['sSearch_'.$i]); 
       foreach ($allDept as $key => $value) { 
        if($key == 0) 
         $sWhere->AND->equalTo("tbl_department.vDeptName", $value); 
        else 
         $sWhere->OR->equalTo("tbl_department.vDeptName", $value); 
       } 
      }elseif($aColumns[$i] == 'vFirst') 
       $sWhere->AND->equalTo("tbl_user.iUserID",$data['sSearch_'.$i]); 
      else 
       $sWhere->AND->like("tbl_bids.".$aColumns[$i], "%" . $data['sSearch_'.$i] . "%"); 
      $select->where($sWhere); // here my where clause is create 
     } 
    } 
    //var_dump($select->getSqlString()); 
    $resultSet = $this->tableGateway->selectWith($select); 
    return $resultSet; 
} 

我也有其它許多領域通過這裏也有paratheses 的同樣的問題,如果沒有任何條件,我可以使用嵌套()和UNNEST()謂詞,但它會告訴我該字符串不是嵌套的錯誤r, 所以請幫助我找到解決方案。 請附上解決方案的例子。

+0

可能重複[ZF2如何orWhere()](http://stackoverflow.com/questions/13056820/zf2-how -to-orwhere) –

回答

0

這裏是一個簡單的例子

$where = new Sql\Where(); 
$where->equalTo('col',thirdVal') 
    ->NEST //start braket 
     ->equalTo('col','someVal') 
     ->OR 
     ->equalTo('col','secondVal') 
    ->UNNEST //close bracet 

希望這將幫助