2

Why did Action Column (view, update, delete) in my Grid View return value id incremental from zero (not return id from database)?

My Controller

public function actionIndex()
{
    $jenjang = Yii::$app->request->get('jenjang');

    $sql = "SELECT * FROM adikbinaan";

    if($jenjang!=0){
        $sql = "SELECT * FROM adikbinaan WHERE adikbinaan.jenjang_id = $jenjang";
    }

    $n = count(CariAdikBinaan::findBySql($sql)->all());

    $adikbinaan = new SqlDataProvider([
        'sql' => $sql,
        'totalCount' => $n,
        'sort' => [
            'attributes' => [
                'ADIKBINAAN_NAMALENGKAP',
            ],
        ],
        'pagination' => [
            'pageSize' => 20,
        ],
    ]);

    $adik = new CariAdikBinaan();

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

My View

    <?= GridView::widget([
    'dataProvider' => $dataProvider,
    'layout'=>"{pager}\n{items}\n{summary}",
    'showFooter'=>true,
    'showHeader'=>true,
    'showOnEmpty'=>false,
    'columns' => [
        'ADIKBINAAN_NAMALENGKAP',
        'ADIKBINAAN_TEMPATLAHIR',
        'ADIKBINAAN_TANGGALLAHIR',
        ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>

When i click View or Update Button from Action Column, the URL returns view?id=0 or update?id=0 not id from database.

How do i fix it?

2 Answers 2

5

You need to declare which column is the identifying column in your SqlDataProvider. Otherwise you just get that serial number. So put the key attribute to the provider:

$adikbinaan = new SqlDataProvider([
    'sql'        => $sql,
    'totalCount' => $n,
    'key'        => 'id'  // <---- here it is, can also be an array of strings if more columns are identifiying ones
    'sort'       => [
        'attributes' => [
            'ADIKBINAAN_NAMALENGKAP',
        ],
    ],
    'pagination' => [
        'pageSize' => 20,
    ],
]);

Does that work for you? I suppose my other answer is less valuable...

Btw: This

$sql = "SELECT * FROM adikbinaan WHERE adikbinaan.jenjang_id = $jenjang";

is dangerous since SQL injection would work. Also the way how you count the records in efficient. Look at the example of the class SqlDataProvider. There COUNT(*) is used which requires quite less data transfer.

Sign up to request clarification or add additional context in comments.

Comments

0

ActionColumn creates the URL by default with method createUrl(). You could override this with declaring an alternative implementation:

'columns' => [
    'ADIKBINAAN_NAMALENGKAP',
    'ADIKBINAAN_TEMPATLAHIR',
    'ADIKBINAAN_TANGGALLAHIR',
    [
        'class'      => 'yii\grid\ActionColumn', 
        'urlCreator' => function($action, $model, $key, $index) {
            return Url::to([
                'someController/' . $action, 
                // not sure: either this
                'id' => $model->fieldThatContainsId,
                // or this:
                'id' => $model['fieldThatContainsId'],
            ]);
        }
    ],
],

I'm posting that without really knowing if it's possible. I haven't tried it. Usually, ActionColumn is used with ActiveRecord instances that you get when you use ActiveDataProvider. If you have such ActiveRecord model instances you could also that provider as an alternative.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.