2017-03-21 52 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));  
相关问题