2017-03-21 92 views
0

我在PHP中使用蒙戈DB驅動程序類像如何過濾PHP中的數據Mongo DB驅動程序使用或條件?

public function fetchStudentsForBroadcast() 
    { 
    $this->collection = $this->db->studentTbl; 
    $batch = (int)$this->batch; 
    $course = $this->course; 
    $year = $this->academicyear; 

    $projection = array("student_id" => 1,"roll_no" => 1,"uploads.profile_pic"=> 1, 
     "first_name" => 1,"middle_name"=>1,"last_name"=>1,"email"=>1, 
     "registration_temp_perm_no"=>1,"dob"=>1,"gender"=>1,"guardian_name"=>1); 

     if ($course != "Select") 
      $orcourse = array('$and' => array(array("batchhistory.course" => new MongoRegex("/$course/i")))); 
     else 
      $orcourse = array('$and' => array(array("batchhistory.course" => new MongoRegex("/$empty/i")))); 

     if ($batch != "Select") 
      $orbatch = array('$and' => array(array("batchhistory.batchid" => new MongoRegex("/$batch/i")))); 
     else 
      $orbatch = array('$and' => array(array("batchhistory.batchid" => new MongoRegex("/$empty/i")))); 

     if ($year != "Select") 
      $oryear = array('$and' => array(array("batchhistory.academic_year" => new MongoRegex("/$year/i")))); 
     else 
      $oryear = array('$and' => array(array("batchhistory.academic_year" => new MongoRegex("/$empty/i")))); 

     $query = array('$and' => array($and, $orbatch, $orcourse, $oryear));   


// $query = array('batchhistory.course'=>''.$course.'' , 'batchhistory.batchid'=>$batch , 'batchhistory.academic_year'=>''.$year.'','is_active'=>1,'has_finished'=>0); 

    $cursor = $this->collection->find($query,$projection); 
    return $cursor; 

} 

其實我試圖弄清楚如何篩選在PHP MongoDB的驅動程序寫入數據的功能。如果$ course包含'Select'以外的內容,那麼過濾應該基於$ course,如果$ course和$ batch包含'Select'以外的值,那麼過濾應該基於$ course和$ batch。如果所有三個都包含「Select」以外的值,則過濾應基於所有三個值。
上述代碼無法正常工作。

請幫忙!!!

回答

0

我已更新條件和查詢部分。

我沒有數據來驗證代碼。將相應地調整答案。

if ($course != "Select") 
    $regcourse = array("batchhistory.course" => new MongoRegex("/$course/i")); 
else 
    $regcourse = array("batchhistory.course" => new MongoRegex("/$empty/i")); 

if ($batch != "Select") 
    $regbatch = array("batchhistory.batchid" => new MongoRegex("/$batch/i")); 
else 
    $regbatch = array("batchhistory.batchid" => new MongoRegex("/$empty/i")); 

if ($year != "Select") 
    $regyear = array("batchhistory.academic_year" => new MongoRegex("/$year/i")); 
else 
    $regyear = array("batchhistory.academic_year" => new MongoRegex("/$empty/i")); 

$query = array('$or' => array($regcourse, $regbatch, $regyear));  
相關問題