2017-09-03 95 views
0

我有一張有三列的表,即研究生ID(grad_id),資格和主管。任務是找出每位主管有多少學生具有不同的資格(博士,碩士,PGD,其他和總計)。問題在於,在生成表格視圖後,我無法過濾出主管的名字,以查看他/她監督各種資格類別的學生人數。如何過濾Yii2中有條件分組的數據GridView

我在視圖中的GridView看起來是這樣的:

GridView::widget([ 
    'dataProvider' => $sup, 
    'filterModel' => $searchModel, 
     'columns' => [ 
['class' => 'yii\grid\SerialColumn'], 
     'supervisor', 
     'total', 
     'phd', 
     'masters', 
     'pgd', 
     'other', 
    ], 
]) 

通過上述,我能夠生成表。但過濾器不改變表的內容,即使刷新頁面和頁面網頁網址更改爲類似(當我搜索大衛):.../index.php?SupervisionSearch%5Bsupervisor%5D=david&SupervisionSearch%5Btotal%5D=&SupervisionSearch%5Bphd%5D=&SupervisionSearch%5Bmasters%5D=&SupervisionSearch%5Bpgd%5D=&r=student%2Fgrad-admissions%2Fsupervision

在SupervisionSearch類我曾嘗試:

public $supervisor; 
public $phd; 
public $masters; 
public $pgd; 
public $other; 
public $total; 

public function rules() 
    { 
     return [ 
      [['supervisor','phd', 'masters', 'pgd', 'other', 'total'], 'safe'], 
      [['phd', 'masters', 'pgd', 'other', 'total'], 'number'], 
      [['supervisor'], 'string'], 
     ]; 
    } 


    public function scenarios() 
    { 
     // bypass scenarios() implementation in the parent class 
     return Model::scenarios(); 
    } 


    public function search($params) 
    { 
$query = (new \yii\db\Query()) 
->select(["supervisor, sum(qualification = 'PhD') as phd", "sum(qualification = 'Masters') as masters", "sum(qualification = 'PGD') as pgd", "sum(qualification <> 'PhD' and qualification <> 'Masters' and qualification <> 'PGD')as other", "count(grad_id) as total"]) 
-> from('grad_students') 
-> where("supervisor <> ''") 
-> orderBy('total desc') 
-> groupBy('supervisor'); 

    $dataProvider = new ActiveDataProvider([ 
      'query' => $query, 
     ]); 
     $this->load($params); 
     if (!$this->validate()) { 
      // uncomment the following line if you do not want to return any records when validation fails 
      // $query->where('0=1'); 
      return $dataProvider; 
     } 

    $query->andFilterWhere(['like', 'supervisor', $this->supervisor]); 
    return $dataProvider; 
    } 

我需要幫助獲取過濾器來搜索出特定的主管人員姓名並顯示他們爲每個資格類別監督的學生人數。提前致謝。

回答

2

你搜索功能 你可以使用的andFilterWhere代替where

public function search($params) 
{ 
$query = (new \yii\db\Query()) 
->select(["supervisor, sum(qualification = 'PhD') as phd", 
    "sum(qualification = 'Masters') as masters", 
    "sum(qualification = 'PGD') as pgd", 
    "sum(qualification <> 'PhD' 
     and qualification <> 'Masters' 
     and qualification <> 'PGD') as other", "count(grad_id) as total"]) 
-> from('grad_students') 
->andFilterWhere(['like', 'supervisor', $this->supervisor]) 
-> orderBy('total desc') 
-> groupBy('supervisor'); 
..... 

,你應該刪除該聲明在模型中的列..你建議立即進行刪除聲明只計算列別名所以儘量評論public $supervisor;

否則您的重新申報隱藏(重新定義)原始值並且不起作用

//public $supervisor; 
public $phd; 
public $masters; 
public $pgd; 
public $other; 
public $total; 
+0

謝謝scaisEdge,我試圖使用'andFilterWhere'而不是'where'作爲建議,但過濾器仍然無法工作。 –

+0

答案更新..試用評論// public $ supervisor;在您的搜索模型 – scaisEdge

+0

再次感謝,我評論說「公共$上司;」但仍然沒有工作。但是,我嘗試了一些工作,以下是我發佈的答案。 –

0

我通過一種解決方案得到了解決方案。我認爲這個問題與原始問題中缺少主鍵有關。我現在所做的,我首先在數據庫中創建了一個視圖,該視圖也沒有主鍵。然後我使用視圖(而不是原始表格)來生成帶有GII的模型。接下來,我將一個primaryKey函數放入模型中,然後使用GII CRUD生成所需的搜索模型,其搜索過濾器現在工作得非常好。 這是我用於生成視圖的代碼(與MySQL使用phpMyAdmin):

create view supervisor_count as select supervisor, sum(qualification = 'PhD') as phd, sum(qualification = 'Masters') as masters, sum(qualification = 'PGD') as pgd, sum(qualification <> 'PhD' and qualification <> 'Masters' and qualification <> 'PGD')as other, count(grad_id) as total from grad_students where supervisor <> '' group by supervisor order by count(grad_id) desc 

這是我曾在SupervisorCount模型以包括得到的主鍵的代碼:

public static function primaryKey(){ 
return 'supervisor'; 
} 

GII CRUD無法在沒有主鍵的情況下運行。所以我在模型中使用了上面的primaryKey函數。但是,當生成CRUD時,首先,表/index.php?r=student%2Fsupervisor-count%2Findex的鏈接不起作用,拋出錯誤 Getting unknown property: app\modules\student\models\SupervisorCount::p。我通過在Gii上重新運行SupervisorCount生成器來解決這個問題,它刪除了我在模型中放置的primaryKey函數。此後,鏈接正常工作。使用生成的SupervisorCountSearch模型的GridView現在具有適用於每個列(Supervisor,Phd,Masters,Pgd,Other,Total)的過濾器。

但是,我仍然想知道是否可以有更好的解決方案,或者使用原始表gradstudents替代mysql生成的supervisor_count視圖的解決方案。