这是表结构:
Table BaseTable
id (primary key) INT
description VARCHAR(255)
Table ChildTable
id (primary key)(foreign key reference to BaseTable) INT
child_property VARCHAR(255)
它实际上是数据库表中的继承关系。
然后我使用gii
为两者生成具有关系函数的模型,并为 ChildTable 生成 CRUD 操作。这是 ChildTable 中的关系函数:
public function getBaseTable()
{
return $this->hasOne(BaseTable::className(), ['id' => 'id']);
}
在 ChildTable 的生成形式中,我想更新description
它的 BaseTable 的属性。这是 ChildTable 的原始形式:
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'child_property')->textInput() ?>
<?php ActiveForm::end(); ?>
尝试1:
我在下面添加代码字段:
<?= $form->field($model, 'baseTable.description')->textInput() ?>
我收到了这个错误:
Getting unknown property: app\models\ChildTable::baseTable.description
尝试2:
我在下面广告代码字段:
<?= $form->field($model->baseTable, 'description')->textInput() ?>
我得到另一个错误:
Call to a member function formName() on null
尝试3:
我使用relation来获取BaseTable模型,代码如下:
<?= $form->field($model->getBaseTable()->one(), 'description')->textInput() ?>
我仍然得到错误:
Call to a member function formName() on null
主意:
我有一个想法,我可以创建一个新的 viewModel 来映射来自BaseTable
和的所有属性ChildTable
。但是我想知道是否有一个快速的解决方案来实现ActiveRecord
基于生成的代码保存多个相关模型gii
?谢谢!