我有一个包含三列的表,即研究生id(grad_id),资格和主管。任务是找出每个主管有多少学生获得各种资格(博士,硕士,PGD,其他和总计)。问题是,在生成表格视图后,我无法过滤出主管人员的姓名,以查看他/她为各种资格类别监督的学生人数。
视图中的我的GridView如下所示:
GridView::widget([
'dataProvider' => $sup,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'supervisor',
'total',
'phd',
'masters',
'pgd',
'other',
],
])
通过上述内容,我可以生成表格。但是过滤器不会更改表的内容,即使刷新页面并且页面Web URL更改为(当我搜索david时):.../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;
}
我需要帮助,让过滤器搜索特定的主管名称,并显示他们为每个资格类别监督的学生人数。提前谢谢。
答案 0 :(得分: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;
答案 1 :(得分:0)
我通过一种解决方案获得了解决方案。我认为问题与原始问题中缺少主键有关。我现在做了什么,我首先在数据库中创建了一个视图,该视图也没有主键。然后我使用视图(而不是原始表)来生成带有GII的模型。接下来,我在模型中放置了一个primaryKey函数,然后使用GII CRUD生成所需的搜索模型,其搜索过滤器现在运行得非常好。 这是我用来生成视图的代码(使用phpmyadmin的mysql):
var periodArray = new Array();
var profitArray = new Array();
var costArray = new Array();
@forelse($salesanalysis as $analysis)
periodArray.push("{{\Carbon\Carbon::parse($analysis->created_at)->format($format)}}");
profitArray.push("{{$analysis->total_profit}}");
costArray.push("{{$analysis->total_cost}}");
@empty
@endforelse
这是我必须包含在SupervisorCount模型中以获取主键的代码:
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
如果没有主键,GII CRUD就无法运行。所以我在模型中使用了上面的primaryKey函数。但是当生成CRUD时,首先,表public static function primaryKey(){
return 'supervisor';
}
的链接不起作用,抛出错误
/index.php?r=student%2Fsupervisor-count%2Findex
。我通过在Gii上重新运行模型Getting unknown property: app\modules\student\models\SupervisorCount::p
生成器解决了这个问题,它删除了我放在模型中的primaryKey函数。此后,链接正常工作。使用生成的SupervisorCount
模型的GridView现在有一个适用于每个列的过滤器(Supervisor,Phd,Masters,Pgd,Other,Total)。
但是,我仍然想知道是否有更好的解决方案,或者使用原始表SupervisorCountSearch
代替mysql生成的gradstudents
视图的解决方案。