Getting Data for Multiple Models

When dealing with some complex data, it is possible that you may need to use multiple different models to collect the user input. For example, assuming the user login information is stored in the user table while the user profile information is stored in the profile table, you may want to collect the input data about a user through a User model and a Profile model. With the Yii model and form support, you can solve this problem in a way that is not much different from handling a single model.

In the following, we will show how you can create a form that would allow you to collect data for both User and Profile models.

First, the controller action for collecting the user and profile data can be written as follows,

  1. namespace app\controllers;
  2. use Yii;
  3. use yii\base\Model;
  4. use yii\web\Controller;
  5. use yii\web\NotFoundHttpException;
  6. use app\models\User;
  7. use app\models\Profile;
  8. class UserController extends Controller
  9. {
  10. public function actionUpdate($id)
  11. {
  12. $user = User::findOne($id);
  13. $profile = Profile::findOne($id);
  14. if (!isset($user, $profile)) {
  15. throw new NotFoundHttpException("The user was not found.");
  16. }
  17. $user->scenario = 'update';
  18. $profile->scenario = 'update';
  19. if (Model::loadMultiple([$user, $profile], Yii::$app->request->post())) {
  20. if ($user->validate() && $profile->validate()) {
  21. $user->save(false);
  22. $profile->save(false);
  23. return $this->redirect(['user/view', 'id' => $id]);
  24. }
  25. }
  26. return $this->render('update', [
  27. 'user' => $user,
  28. 'profile' => $profile,
  29. ]);
  30. }
  31. }

In the update action, we first load the $user and $profile models to be updated from the database. We then call [[yii\base\Model::loadMultiple()]] to populate these two models with the user input. If successful we will validate the two models and save them. Otherwise we will render the update view which has the following content:

  1. <?php
  2. use yii\helpers\Html;
  3. use yii\widgets\ActiveForm;
  4. $form = ActiveForm::begin([
  5. 'id' => 'user-update-form',
  6. 'options' => ['class' => 'form-horizontal'],
  7. ]) ?>
  8. <?= $form->field($user, 'username') ?>
  9. ...other input fields...
  10. <?= $form->field($profile, 'website') ?>
  11. <?= Html::submitButton('Update', ['class' => 'btn btn-primary']) ?>
  12. <?php ActiveForm::end() ?>

As you can see, in the update view you would render input fields using two models $user and $profile.