I am new to yii2, so in yii2 basic how to view and update records without id value in the url during CRUD
Code:
public function actionView($id)
{
$model = $this->findModel($id);
return $this->render('view', [
'model' => $model
]);
}
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['index']);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
In your view. Create an update button
echo
Html::beginForm(['/model/update'], 'post',['id' => 'update-form']) .
'<input type="hidden" name="id" value="'.$model->id.'">
<a href="javascript:{}" onclick="document.getElementById(\'update-form\').submit();
return false;">Update</a>'.
Html::endForm();
Then in your controller
public function actionUpdate()
{
if ( Yii::$app->request->post() ) {
if ( isset (Yii::$app->request->post()['Model']['id']) ) {
$id = Yii::$app->request->post()['Model']['id'];
} else {
$id = Yii::$app->request->post()['id'];
}
$model = $this->findModel($id);
}
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['index']);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
We can access view and update page without id value in the Url by creating Modal to view and update button.