When I use gii to create CRUD behaviour the update form and action are working out of the box. I don’t know how it exactly works. The action of the form is automatically generated and actionUpdate($id) gets the right id.
Now I’d like to add another form in the update view. This form should call another action (same controller) and then forward back to to the update view. My questions:
How do I define the target action?
And how do I pass the model id so actionDoSomething($id) will get this id as argument? Or does this happen automatically with defining the action?
Is it possible to declare more action parameters? Or is only one supported?
I’m confused how views and controllers work together.
Actually, my question wasn’t about a gii itself. I don’t want to change its behaviour. I just want to create an action in my existing controller and view.
For example:
class LanguageController {
//Generated action, unmodified
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
//My new action
public function actionSomethingElse($id) {
//do something
return $this->redirect(['update', 'id' => $id]);
}
}
and in the update.php view I have added the ‘My own form’ section and didn’t change anything else:
I’m not sure. Do I have to specify the action in ActiveForm::begin()? And how will the model id get passed to the action method? I’m dealing here with the same model that is used in _form.php.
Btw: I know it is a form on its own - all data of the other form (in _form.php) won’t be submitted. This could be a problem. However, first of all I just want to know how to add a second form.