2013-10-29 38 views
0

我需要根據用戶選擇過濾查詢中的結果。我發送一個數組,其中包含爲查詢選擇的所有選項。動態查詢在教條與AND /或

這是數組的樣子:

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; 
    }  

} 

它返回的是一個ba D所形成的結果,這裏是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) 

而且,它說,「女性」是一個未定義的索引(如果用戶選擇這個選項,這不出現)

+0

我建議改變陣列的填充方式,使得例如,如果選擇重新「激活」的存在,你生產「和積極的設置參數in(<值列表>)「。做到這一點,並建立你的動態查詢將兒童玩。 –

+0

你說我應該向查詢發送一個2D數組嗎? – Limon

回答

0

可以使用或者這些查詢構建器中的哪裏或哪些地方? 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'));