2

I'm using Model Search to filter and query all data into controller and send back data into View. The query is fine, but in View can't show data. And I don't know how to fix it?

This is my Model search:

$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,
]);

In my Controller:

    public function actionIndex()
        {
            $searchModel = new WorkloadSearch();
            $dataProvider = $searchModel->searchWorkloadofUser(Yii::$app->request->queryParams);

            return $this->render('index', [
                'searchModel' => $searchModel,
                'dataProvider' => $dataProvider,
            ]);
        }

And in my View Index page:

$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,
    ]); ?>
Francis
  • 288
  • 1
  • 5
  • 17
  • What do you mean by "can't show data"? There is no GridView at all or it shows 0 records? Also please provide full code of displaying GridView in the view. – arogachev Oct 26 '15 at 03:09
  • I mean didn't show data, but the query is query good. The view page show nomally without data – Francis Oct 26 '15 at 03:12
  • Please add the rest of the code for displaying GridView. – arogachev Oct 26 '15 at 03:12
  • This is the link for full code of View page: http://pastebin.com/fWiemhDr – Francis Oct 26 '15 at 03:14
  • It's better to add code right to the answer rather than posting it at external resources, especially when it's not so long. But I can see now that GridView is displayed normally (at first I thought maybe you forgot `echo`), so the problem is related with 0 records. I recommend to check generated query and compare it to desired. Use debug console for it. – arogachev Oct 26 '15 at 03:17
  • Like I said before, view page show normally, but data from database that I query can't show in gridview of view page. I've checked query statement, this show fine. – Francis Oct 26 '15 at 03:19

1 Answers1

1

What i've found in your code that you are using all() method which is not required by ActiveDataProvider because all() method returns all the rows but you need to pass query object to 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,
]);

After that you can print $dataProvider->models for debugging purpose that you are getting rows from database. If you are getting rows means you are now can pass $dataProvider object to GridView.

Touqeer Shafi
  • 5,084
  • 3
  • 28
  • 45
  • I've removed `all()` function and print `$dataprovider->models` and I felt good, but when I pass to `$dataProvider` in Index, it don't show data. I don't understand how? – Francis Oct 26 '15 at 08:58