Mark C.
Mark C.

Reputation: 606

Yii2 How to display a single record from a related model's gridview

I have two tables, 'employee' and 'employee_payrecords'.

I have a view called 'dashboard' that is based on the Employee model. Employee is related to employee_payrecords (n:m). On the dashboard, I have a gridview widget that shows all payrecords. I would like to be able to click on 'view' (in the action column of the Gridview widget) for the payrecord, and display the payrecord in a new window (1 payrecord). Currently if I hover over the payrecord's 'view' action it displays the correct id of the payrecord in the browser's bottom preview bar but if I click on the 'view' action item it gives me a 'Not Found' error. I just simply want to display the payrecord in it's own view. What am I missing?

In the EmployeeController, I have an action to view paycheck from the EmployeePayrecord model (which is related via the Employee model):

public function actionViewPaycheck($id)
{
    $model = EmployeePayrecord::findOne($id);

    return $this->render('viewPaycheck', [
        'model' => $model,
    ]);
}

In the Employee model I have a method to retreive all payrecords (for the Gridview in 'dashboard' view):

public function getEmployeePayrecords()
{
    return $this->hasMany(\frontend\models\EmployeePayrecord::className(), ['employee_id' => 'id']);
}

And in the 'dashboard' view I have created what I think is the correct URL as previously mentioned above, it does display the correct record number for the payrecord in the browser's preview of the link (before I click the 'view' action button.) Also, not shown here is the view-paycheck view (viewPaycheck.php) which is what the url refers to, to display the payrecord:

[
     'class' => 'yii\grid\ActionColumn',
     'template' => '{view}',
     'header' => 'Actions',
     'buttons' => [
           'view' => function ($url, $model) {
                return Html::a(
                   '<span>view</span>',
                   ['employee/view-paycheck', 'id' => $model->id],
                   ['title' => Yii::t('app', 'View Paycheck')]
                 );
             }
      ],
],

Action rendering Dashboard view:

public function actionDashboard($id)
{
    /**
     * Use the employee layout for 'dashboard'
     */
    $this->layout = 'employee';

    $model = $this->findModel($id);

    $providerEmployeePayrecords = new \yii\data\ArrayDataProvider([
        'allModels' => $model->employeePayrecords,
        'pagination' => [
            'pageSize' => 5,
        ]
    ]);

    return $this->render('dashboard', [
        'model' => $model,
        'providerEmployeePayrecords' => $providerEmployeePayrecords,

    ]);
}

Upvotes: 2

Views: 719

Answers (1)

Bizley
Bizley

Reputation: 18021

Since you are using this in one button only there is no point of modifying whole urlCreator.

[
    'class' => 'yii\grid\ActionColumn',
    'template' => '{view}',
    'header' => 'Actions',
    'buttons' => [
        'view' => function ($url, $model) {
            return Html::a(
                '<span>view</span>', 
                ['employee/view-paycheck', 'id' => $model->id], 
                ['title' => Yii::t('app', 'View Paycheck')]
            );
        }
    ],
],

UPDATE

After long debugging process we have finally got to the core of the error.

There were two GridViews (Kartik's version) in the view, both using Pjax (through pjax setting and not the Pjax wrapper) and apparently second one, which was using different link for the view button, has been loaded somehow into the first pjax container, changing the target URL for the button link.

pjaxSettings have been set to use different ID for container so it should not be the case but looks like there is a bug with this.

Removing the pjax settings from GridViews and using the standard Pjax wrapper (Pjax::begin() and Pjax::end()) helped and fixed the problem.

Upvotes: 1

Related Questions