1

I want my query like this:

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

But i can't find the way to get parantheses in my query,there are mutiple condition and loop will be there to make where clause.. here is my code

    $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;
}

I have others many fields to pass through where which also have same problem of paratheses if there is no any condition i can use nest() and unnest() predicate , but it will show me that string is not nested error, So pls help me to find the solution. Pls attach example with solution.

UiUx
  • 967
  • 2
  • 14
  • 25
user3157253
  • 83
  • 1
  • 6
  • possible duplicate of [ZF2 How to orWhere()](http://stackoverflow.com/questions/13056820/zf2-how-to-orwhere) – Tim Burch Feb 07 '14 at 14:06

1 Answers1

3

here is a short example

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

hope this will help

Unex
  • 1,747
  • 13
  • 17