我使用模型搜索过滤并查询所有数据到控制器并将数据发送回View。查询很好,但在View中无法显示数据。而且我不知道如何解决它?
这是我的模型搜索:
$user_id = Yii::$app->user->id;
$subquery = Workload::find()->select(['p.id', 'p.project_name', 'w.user_id', 'w.commit_time', 'w.comment', 'w.workload_type'])
->from(['project as p', 'workload as w'])
->where(['and','w.user_id = '.$user_id, 'p.id = w.project_id']);
$query = Workload::find()
->select(['workloadTeam.project_id', 'wp.project_name', 'workloadTeam.user_id', 'workloadTeam.commit_time', 'workloadTeam.week','workloadTeam.workload_type', 'workloadTeam.comment'])
->from(['wp' => $subquery]);
$query->join('INNER JOIN', 'workload as workloadTeam', 'wp.id = workloadTeam.project_id')->all();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
在我的控制器中:
public function actionIndex()
{
$searchModel = new WorkloadSearch();
$dataProvider = $searchModel->searchWorkloadofUser(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
在我的查看索引页面中:
$gridColumns = [
[
'class' => 'yii\grid\SerialColumn',
'contentOptions' => ['style' => 'width: 40px;']
],
[
'attribute' => 'user_id',
'label' => Yii::t('app', 'Members Alias'),
'value' => 'profile.full_name',
'filter' => \app\modules\admin\models\User::profileDropDown(),
'contentOptions' => ['style' => 'width: 150px;', 'class' => 'select2'],
'filterOptions' => ['data-widget' => 'select2'],
'format' => 'html',
],
[
'attribute' => 'project_id',
'label' => Yii::t('app', 'Assigned Projects'),
'value' => 'project.project_name',
'filter' => \app\modules\admin\models\Project::projectWorkloadMemberDropdown(),
'contentOptions' => ['style' => 'width: 150px;', 'class' => 'select2', 'placeholder' => 'Select your projects'],
'filterOptions' => ['data-widget' => 'select2'],
'format' => 'html',
],
[
'label' => 'Committed (hr.)',
'value' => function ($model) {
return $model->commit_time;
}
],
[
'label' => 'Burned Amount (hr.)',
'value' => function ($model) {
return WorkLog::getTotalBurnedHoursofUser($model->user_id, $model->project_id);
},
],
[
'attribute' => 'week',
'filterInputOptions' => ['class' => 'form-control datepicker'],
'value' => function ($model) {
return $model->week;
},
'label' => Yii::t('app', 'From Date'),
],
[
'label' => 'Comment',
'value' => function ($model) {
return $model->comment;
}
],
[
'label' => 'Workload Type',
// 'attribute' => 'workload_type',
'filter' => \app\modules\admin\models\Workload::WorkloadTypeDropdownList(),
'value' => function ($model) {
return \app\modules\admin\models\Workload::$workload_type_label[$model->workload_type];
}
]
];
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
]); ?>
答案 0 :(得分:1)
我在您的代码中发现您使用的all()
方法是ActiveDataProvider
不需要的,因为all()
方法会返回所有行,但您需要将查询对象传递给ActiveDataProvider
$user_id = Yii::$app->user->id;
$subquery = Workload::find()->select(['p.id', 'p.project_name', 'w.user_id', 'w.commit_time', 'w.comment', 'w.workload_type'])
->from(['project as p', 'workload as w'])
->where(['and','w.user_id = '.$user_id, 'p.id = w.project_id']);
$query = Workload::find()
->select(['workloadTeam.project_id', 'wp.project_name', 'workloadTeam.user_id', 'workloadTeam.commit_time', 'workloadTeam.week','workloadTeam.workload_type', 'workloadTeam.comment'])
->from(['wp' => $subquery]);
$query->join('INNER JOIN', 'workload as workloadTeam', 'wp.id = workloadTeam.project_id');
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
之后,您可以打印$dataProvider->models
以进行调试,以便从数据库获取行。如果您获得行意味着您现在可以将$dataProvider
对象传递给GridView
。