微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐