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 に答える