我有一張有三列的表,即研究生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;
}
我需要幫助獲取過濾器來搜索出特定的主管人員姓名並顯示他們爲每個資格類別監督的學生人數。提前致謝。
謝謝scaisEdge,我試圖使用'andFilterWhere'而不是'where'作爲建議,但過濾器仍然無法工作。 –
答案更新..試用評論// public $ supervisor;在您的搜索模型 – scaisEdge
再次感謝,我評論說「公共$上司;」但仍然沒有工作。但是,我嘗試了一些工作,以下是我發佈的答案。 –