带有AND / OR的学说中的动态查询

时间:2013-10-29 20:32:51

标签: php sql codeigniter doctrine dql

我需要根据用户选择过滤查询中的结果。我发送一个数组,其中包含所有选择的选项。

这就是数组的样子:

array (size=6)
'registered' => int 2
'active' => int 1
'notactive' => int 0
'preregistered' => int 1
'male' => string 'm' (length=1)
'female' => string 'f' (length=1)

我需要执行此操作(假设用户选择了此选项):

(registered OR preregistered) AND (active OR notactive) AND (male)

到目前为止,这是我的代码,花了很多时间和精力,但它没有给我正确的结果:

public function customReport($data){
        // var_dump($data);die();

    $this->qb = $this->em->createQueryBuilder();
    $andX = $this->qb->expr()->andX();

    $this->qb->select('u')
    ->from('models\User','u');          

    foreach($data as $key=>&$value){

        if($key == "registered"){
            if(in_array("preregistered", $data)){
                $condition = $this->qb->expr()->orX(
                    $this->qb->expr()->eq('u.status', '?5'),
                    $this->qb->expr()->eq('u.status', '?6')
                    );
            }else{
                $condition = $this->qb->expr()->eq('u.status', '?5');
            }
            $andX->add($condition);
        }else{
            if($key == "preregistered"){
                $condition = $this->qb->expr()->eq('u.status','?6');
            }
            $andX->add($condition);
        }

        if($key == "active"){
            if(in_array("notactive", $data)){
                //va un OR entre estas dos condiciones
                // $condition = ('u.active = 1 OR u.active = 0');
                $condition = $this->qb->expr()->orX(
                    $this->qb->expr()->eq('u.active', '?1'),
                    $this->qb->expr()->eq('u.active', '?2')
                    );
                    // var_dump($condition);die();
            }else{
                $condition = $this->qb->expr()->eq('u.active', '?1');
            }
            $andX->add($condition);
        }else{
            if($key == "notactive"){
                $condition = $this->qb->expr()->eq('u.active', '?2');
            }
            $andX->add($condition);
        }

        if($key == "male"){
            if(in_array("female", $data)){
                $condition = $this->qb->expr()->orX(
                    $this->qb->expr()->eq('u.gender', '?3'),
                    $this->qb->expr()->eq('u.gender', '?4')
                    );
            }else{
                $condition = $this->qb->expr()->eq('u.gender', '?3');
            }
            $andX->add($condition);
        }else{
            if($key == "female"){
                $condition = $this->qb->expr()->eq('u.gender', '?4');
            }
            $andX->add($condition);
        }
        // var_dump($condition);die();
    }

    $this->qb->add('where', $andX);

    if(in_array('active', $data)){
        $this->qb->setParameter(1,$data['active']);
    }
    if(in_array('notactive', $data)){
        $this->qb->setParameter(2,$data['notactive']);
    }
    if(in_array('male', $data)){
        $this->qb->setParameter(3, $data['male']);
    }
    if(in_array("female", $data)){
        $this->qb->setParameter(4, $data['female']);
    }
    if(in_array('registered', $data)){
        $this->qb->setParameter(5,$data['registered']);
    }
    if(in_array('preregistered', $data)){
        $this->qb->setParameter(6,$data['preregistered']);
    }


    $query = $this->qb->getQuery();

    var_dump($query);die();
    $obj = $query->getResult();


    if (!empty($obj)){
        return $obj;

        return false;
    }       

}

它返回的是一个糟糕的结果,这里是ir:

的一部分
string 'SELECT u FROM models\User u WHERE (u.status = ?5 OR u.status = ?6) AND (u.status = ?5 OR u.status = ?6) AND (u.status = ?5 OR u.status = ?6) AND (u.status = ?5 OR u.status = ?6) AND (u.active = ?1 OR u.active = ?2) AND (u.active = ?1 OR u.active = ?2) AND (u.active = ?1 OR u.active = ?2) AND u.active = ?2 AND u.active = ?2 AND u.status = ?6 AND u.status = ?6 AND u.status = ?6 AND u.status = ?6 AND u.status = ?6 AND (u.gender = ?3 OR u.gender = ?4)' (length=451)

而且,它说“女性”是一个未定义的索引(如果用户选择此选项,则不会显示)

1 个答案:

答案 0 :(得分:0)

你可以在那些查询构建器中使用orWhere或者Where吗? ref

// NOTE: ->where() overrides all previously set conditions
    //
    // this will remove all other orWhere or andWhere before it. so use it only initially
    public function where($where);

    // Example - $qb->andWhere($qb->expr()->orX($qb->expr()->lte('u.age', 40), 'u.numChild = 0'))
    public function andWhere($where);

    // Example - $qb->orWhere($qb->expr()->between('u.id', 1, 10));
    public function orWhere($where);

您也可以尝试在expr上设置参数

    $this->qb->expr()->orX(
                    $this->qb->expr()->eq('u.gender', '?3'),
                    $this->qb->expr()->eq('u.gender', '?4'))
                    ->setParameters(array(3 => 'value for ?3', 4 => 'value for ?4'));