0

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

这是数组的样子:

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)

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

4

1 回答 1

0

您可以在那些查询构建器中使用 orWhere 或 andWhere 吗?参考

// 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'));
于 2014-01-08T20:47:37.523 回答