2017-03-21 7 views
0

私は実際に私は、PHPのMongoDBドライバでデータをフィルタリングする方法を整理しようとしています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; 

} 

のようなのmongo DBドライバクラスを使用して、PHPの関数を書かれています。 $ courseに 'Select'以外のものが含まれている場合、フィルタリングは$ courseに基づいている必要があります。また、$ courseと$ batchに 'Select'以外の値が含まれている場合、フィルタリングは$ courseと$ batchに基づいて行われます。 3つのすべてに「選択」以外の値が含まれている場合、フィルタリングは3つすべてに基づいて行われる必要があります。
上記のコードは機能しません。

助けてください!

答えて

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));  
関連する問題