如何以编程方式创建与Zend \ Db \ Sql组件的嵌套where子句?

时间:2014-02-25 09:15:13

标签: zend-framework

我尝试使用以下代码来完成它,但是不需要的函数会返回错误:

foreach($params as $key=>$param) {
    if(strpos($param, ',') !== false) {
        $where = new \Zend\Db\Sql\Where();

        $where->nest();
        $param_arr = explode(',', $param);

        $entered_once = 0;
        foreach($param_arr as $p) {
            $where->equalTo($key, $p);
            if($entered_once < count($param_arr)) {
                $where->or;
            }
            $entered_once++;
        }
        $where->unnest();
        $select->where($where);
    }
    else {
        $select->where(array($key => $param));
    }
}   

1 个答案:

答案 0 :(得分:0)

我已经弄清楚我做错了什么:我没有意识到nest()函数返回了一个全新的PredicateSet,这就是我需要调用的东西。解决方案如下:

foreach($params as $key=>$param) {
    if(strpos($param, ',') !== false) {
        $where = new \Zend\Db\Sql\Where();

        $predicate_set = $where->nest();
        $param_arr = explode(',', $param);

        $entered_once = 0;
        foreach($param_arr as $p) {
            $predicate_set->equalTo($key, $p);
            if($entered_once < count($param_arr)) {
                $predicate_set->or;
            }
            $entered_once++;
        }

        $predicate_set->unnest();
        $select->where($where);
    }
    else {
        $select->where(array($key => $param));
    }
}