Reputation: 1
I have been working through the getting started guide for Yii. After generating the CRUD code I receive an error when clicking on the "View/Edit/Delete" buttons. It appears that the generated links are incorrect in that they reference "id" rather than "code" (which is the primary key).
E.g the View link is
http://localhost/yii/demo/basic/web/index.php?r=country%2Fview&id=AU
which generates the error
Bad Request (#400)
Missing required parameters: code
when I manually change the link to
http://localhost/yii/demo/basic/web/index.php?r=country%2Fview&code=AU
it works correctly. Why is it referencing "id"?
I also had to modify the CountryController.php file, findModel function which was again referencing "id"
if (($model = Country::findOne($id)) !== null) {
when changed to "code" this worked.
protected function findModel($code)
{
if (($model = Country::findOne($code)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
Does anyone have any ideas, the db is MySQL and the Code column is defined as the Primary Key.
Upvotes: 0
Views: 110
Reputation: 104
This is a bug in the current yii code.
It expects ID to be the primary key always
A way of fixing it would be to remove the action column from the gridview in index.php and add
[
'class' => 'yii\grid\ActionColumn',
'header' => 'Actions',
'headerOptions' => ['style' => 'color:#337ab7'],
'template' => '{view}{update}{delete}',
'urlCreator' => function ($action, $model, $key, $index) {
if ($action === 'view') {
$url ='index.php?r=country/view&code='.$model->code;
return $url;
}
if ($action === 'update') {
$url ='index.php?r=country/update&code='.$model->code;
return $url;
}
if ($action === 'delete') {
$url ='index.php?r=country/delete&code='.$model->code;
return $url;
}
}
],
Just got done going through old answers regarding how to change gridview to get to this.
Upvotes: 0