php – 嵌套在zend framework 2.2中的where子句

我需要这样的查询

SELECT tbl_leads . * , tbl_lead_category.vLeadCategoryName AS vLeadCategoryName, tbl_lead_category.vLeadCategoryIcon AS vLeadCategoryIcon, Concat( vFirst, ' ', vLast ) AS vFirst
FROM tbl_leads
LEFT JOIN tbl_lead_category ON tbl_lead_category.iLeadCategoryID = tbl_leads.iLeadCategoryID
LEFT JOIN tbl_user ON tbl_user.iUserID = tbl_leads.iUserID
WHERE (
tbl_leads.eDeleted = '0'
)
AND (
tbl_leads.iUserID = '1'
)
AND (
vCompanyName LIKE '%t%'
OR vCompanyUrl LIKE '%t%'
OR vPersonName LIKE '%t%'
OR vDesignationName LIKE '%t%'
OR vSkypeID LIKE '%t%'
OR vEmailID LIKE '%t%'
OR vPhoneNumber LIKE '%t%')

我的代码就像:

public function fetchAll($data, $aColumns, $iUserID = "")
{
    $select = $this->tableGateway->getsql()->select();
    $select->columns(array('*'));
    $select->join('tbl_lead_category', 'tbl_lead_category.iLeadCategoryID = tbl_leads.iLeadCategoryID', array('vLeadCategoryName', 'vLeadCategoryIcon'), "LEFT")
            ->join('tbl_user', 'tbl_user.iUserID  = tbl_leads.iUserID',array("vFirst"=>new \Zend\Db\sql\Predicate\Expression("Concat(vFirst,' ',vLast)")),"LEFT");

    $where = new \Zend\Db\sql\Where();
    $where->nesT->equalTo('tbl_leads.eDeleted','0')->UNnesT; 

    if(isset($iUserID) && $iUserID != "")
        $where->AND->nesT->equalTo ('tbl_leads.iUserID', $iUserID)->UNnesT;

    // start for searching
    if (isset($data['sSearch']) && $data['sSearch'] != "") {
        for ($i = 0; $i < count($aColumns) - 1; $i++) {
                $where->OR->like($aColumns[$i], "%" . $data['sSearch'] . "%");
        }
    }

/*i have tried this, but its giving error: not nested*****
    if (isset($data['sSearch']) && $data['sSearch'] != "") {
    $where->AND->nesT;  
        for ($i = 0; $i < count($aColumns) - 1; $i++) {
                $where->OR->like($aColumns[$i], "%" . $data['sSearch'] . "%");
    // how can i get  parentheses to my where which i am searching  
        }
    $where->UNnesT;
    } ***************/
    $select->where($where);
    var_dump($select->getsqlString());
    $resultSet = $this->tableGateway->selectWith($select);
    return $resultSet;
}

如果我使用嵌套而不能通过使用for循环和if语句创建它,
如果我有这样的自定义查询,我该如何进行查询,
没有括号我的查询没有意义.

解决方法:

我认为追随更好. nesT将打开支架UNnesT将关闭支架

$where = new sql\Where();
$where->nesT//start braket
->equalTo('col','someVal')
->OR
->equalTo('col','secondVal')
->UNnesT //close bracet

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...