@@ -115,14 +115,28 @@ Handling multiple models with a single form
...
@@ -115,14 +115,28 @@ Handling multiple models with a single form
> Note: This section is under development.
> Note: This section is under development.
Sometimes you need to handle multiple models of the same kind in a single form. For example, multiple settings where
Sometimes you need to handle multiple models of the same kind in a single form. For example, multiple settings where
each setting is stored as name-value and is represented by `Setting` model. This kind of form is also often
each setting is stored as a name-value pair and is represented by a `Setting`[active record](db-active-record.md) model.
referred to as "tabular input". In contrast to this, handling different models of different kind, is handled in the section
This kind of form is also often referred to as "tabular input".
In contrast to this, handling different models of different kind, is handled in the section
[Complex Forms with Multiple Models](input-multiple-models).
[Complex Forms with Multiple Models](input-multiple-models).
The following shows how to implement tabular input with Yii.
The following shows how to implement tabular input with Yii.
Let's start with controller action:
There are three different situations to cover, which have to be handled slightly different:
- Updating a fixed set of records from the database
- Creating a dynamic set of new records
- Updating, creating and deleting of records on one page
In contrast to the single model forms explained before, we are working with an array of models now.
This array is passed to the view to display the input fields for each model in a table like style and we
will use helper methods of [[yii\base\Model]] that allow loading and validating multiple models at once:
-[[yii\base\Model::loadMultiple()|Model::loadMultiple()]] load post data into an array of models.
-[[yii\base\Model::validateMultiple()|Model::validateMultiple()]] validates an array of models.
### Updating a fixed set of records
Let's start with the controller action:
```php
```php
<?php
<?php
...
@@ -146,7 +160,6 @@ class SettingsController extends Controller
...
@@ -146,7 +160,6 @@ class SettingsController extends Controller
foreach($settingsas$setting){
foreach($settingsas$setting){
$setting->save(false);
$setting->save(false);
}
}
return$this->redirect('index');
return$this->redirect('index');
}
}
...
@@ -155,10 +168,12 @@ class SettingsController extends Controller
...
@@ -155,10 +168,12 @@ class SettingsController extends Controller
}
}
```
```
In the code above we're using `indexBy` when retrieving models from the database to populate an array indexed by model ids.
In the code above we're using [[yii\db\ActiveQuery::indexBy()|indexBy()]] when retrieving models from the database to populate an array indexed by models primary keys.
These will be later used to identify form fields. `loadMultiple` fills multiple models with the form data coming from POST
These will be later used to identify form fields. [[yii\base\Model::loadMultiple()|Model::loadMultiple()]] fills multiple
and `validateMultiple` validates all models at once. In order to skip validation when saving we're passing `false` as
models with the form data coming from POST
a parameter to `save`.
and [[yii\base\Model::validateMultiple()|Model::validateMultiple()]] validates all models at once.
As we have validated our models before, using `validateMultiple()`, we're now passing `false` as
a parameter to [[yii\db\ActiveRecord::save()|save()]] to not run validation twice.