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)
It's a inheritance relation in database table actually.
Then I use gii
to generate the models with relation function for both, and CRUD operation for ChildTable. Here is the relation function in ChildTable:
public function getBaseTable()
{
return $this->hasOne(BaseTable::className(), ['id' => 'id']);
}
In the generated form of ChildTable, I'd like to update the property description
of it's BaseTable. Here is the original form of ChildTable:
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'child_property')->textInput() ?>
<?php ActiveForm::end(); ?>
Try 1:
I add code field below:
<?= $form->field($model, 'baseTable.description')->textInput() ?>
I got this error:
Getting unknown property: app\models\ChildTable::baseTable.description
Try 2:
I ad code field below:
<?= $form->field($model->baseTable, 'description')->textInput() ?>
I got another error:
Call to a member function formName() on null
Try 3:
I use relation to get the BaseTable model, code as below:
<?= $form->field($model->getBaseTable()->one(), 'description')->textInput() ?>
I still got the error:
Call to a member function formName() on null
Idea:
I got a idea that I could create a new viewModel to mapping all the property from BaseTable
and ChildTable
. But I'd like to know wether there is a fast solution to implement the saving multiple related ActiveRecord
models base on the code generated by gii
? Thanks!