How to use two models in one view

can someone please clarify how i can post to two different tables using one single form in yii2

Make two different objects of model and use them in form.

kindly [InsaneSkull] Email me @ muiaalex77@gmail.com

hi am completely stuck,i dont know how i can share my code you tell me where the error is?

in the site controller i have the action for bank and contact as follows public function actionContact()
{
$model = new Contact();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
Yii::$app->session->setFlash(‘contactFormSubmitted’);
return $this->render(‘contact’, [
‘model’ => $model,
]);
} else {
return $this->render(‘contact’, [
‘model’ => $model,
]);
}

}
public function actionBank()
{
$model = new actionBank();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
Yii::$app->session->setFlash(‘contactFormSubmitted’);
return $this->render(‘bank’, [
‘model’ => $model,
]);
} else {
return $this->render(‘bank’, [
‘model’ => $model,
]);
}

}

in the models i have both bank and contact model as follows

<?php namespace app\models; use Yii; /** * This is the model class for table "bank". * * @property int $id * @property string $BankName * @property string $BankBranch * @property string $AccountName * @property string $AccountNumber */ class Bank extends \yii\db\ActiveRecord { /** * {@inheritdoc} */ public static function tableName() { return 'bank'; } /** * {@inheritdoc} */ public function rules() { return [ [['id', 'BankName', 'BankBranch', 'AccountName', 'AccountNumber'], 'required'], [['id'], 'integer'], [['BankName', 'BankBranch', 'AccountName', 'AccountNumber'], 'string', 'max' => 50], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'BankName' => 'Bank Name', 'BankBranch' => 'Bank Branch', 'AccountName' => 'Account Name', 'AccountNumber' => 'Account Number', ]; } } <?php namespace app\models; use Yii; /** * This is the model class for table "details". * * @property int $id * @property string $FirstName * @property string $LastName * @property string $IdNumber * @property string $Email * @property int $PhoneNumber * @property string $PostalAdress * @property string $PostalCode * @property string $City * @property string $Region * @property string $Country */ class Contact extends \yii\db\ActiveRecord { /** * {@inheritdoc} */ public static function tableName() { return 'details'; } /** * {@inheritdoc} */ public function rules() { return [ [['id', 'FirstName', 'LastName', 'IdNumber', 'Email', 'PhoneNumber', 'PostalAdress', 'PostalCode', 'City', 'Region', 'Country'], 'required'], [['id', 'PhoneNumber'], 'integer'], [['FirstName', 'LastName', 'Email', 'PostalAdress', 'PostalCode', 'City', 'Region', 'Country'], 'string', 'max' => 50], [['IdNumber'], 'string', 'max' => 10], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'FirstName' => 'First Name', 'LastName' => 'Last Name', 'IdNumber' => 'Id Number', 'Email' => 'Email', 'PhoneNumber' => 'Phone Number', 'PostalAdress' => 'Postal Adress', 'PostalCode' => 'Postal Code', 'City' => 'City', 'Region' => 'Region', 'Country' => 'Country', ]; } } in the view i have a form.php as follows <?php /* @var $this yii\web\View */ use yii\bootstrap\Tabs; use yii\bootstrap\ActiveForm; use yii\helpers\Url; use yii\helpers\Html; $this->title = 'Sign-Up Form'; ?> <?php $form = ActiveForm::begin(['id' => 'contact-form']); ?> <?= Html::img('@web/images/1.jpg', [ 'alt' => 'pic not found', 'width' => '100px', 'height' => '60px' ]); ?>

Umoja Magharibi SIgn-Up Form


<?= $form->field($model, 'FirstName') ?> <?= $form->field($model, 'LastName') ?> <?= $form->field($model, 'IdNumber') ?> <?= $form->field($model, 'Email') ?> <?= $form->field($model, 'PhoneNumber') ?> <?= $form->field($model, 'PostalAdress') ?> <?= $form->field($model, 'PostalCode') ?> <?= $form->field($model, 'City') ?> <?= $form->field($model, 'Region') ?> <?= $form->field($model, 'Country') ?> <?= $form->field($model, 'BankName') ?> <?= $form->field($model, 'BankBranch') ?> <?= $form->field($model, 'AccountName') ?> <?= $form->field($model, 'AccountNumber') ?>
</div>

</div>

    </div>

now i want to save the data for bank in the details table and data fro contact in the bank table. someone help please

Hi @AlexCharles

The following section of the guide explains exactly what you want to do. Take a look at it.