Yii2 - Saving multiple related ActiveRecord models in one form

Here is the table structure:

Table BaseTable
    id (primary key) INT
    description VARCHAR(255)

Table ChildTable
    id (primary key)(foreign key reference to BaseTable) INT
    child_property VARCHAR(255)

This is actually an inheritance relationship in a database table.

Then I use giito create models with a relationship function for both and for the CRUD operation for ChildTable. Here is the relationship function in ChildTable:

public function getBaseTable()
{
    return $this->hasOne(BaseTable::className(), ['id' => 'id']);
}

In the generated ChildTable form, I would like to update the property of descriptionits BaseTable. Here is the original ChildTable form:

<?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'child_property')->textInput() ?>

<?php ActiveForm::end(); ?>

Try 1:

I am adding the following code field:

<?= $form->field($model, 'baseTable.description')->textInput() ?>

I got this error:

Getting unknown property: app\models\ChildTable::baseTable.description

Try 2:

Below is the ad code field:

<?= $form->field($model->baseTable, 'description')->textInput() ?>

I got another error:

Call to a member function formName() on null

Try 3:

I use the relation to get the BaseTable model, as shown below:

<?= $form->field($model->getBaseTable()->one(), 'description')->textInput() ?>

I still have an error:

Call to a member function formName() on null

Idea:

, viewModel BaseTable ChildTable. , ActiveRecord , gii? !

+4
1

:

<?= $form->field($model->baseTable, 'description')->textInput() ?>

, baseTable - . ( null, - ).

, , .

( , null).

: $model->getBaseTable()->one() , $model->baseTable. .

- .

:

save() .

+6

All Articles