我正在动态创建表格。如果我创建了表Schools和Classes。如何为它们创建模型并指定它们之间的关系。
我搜索但没有得到任何关于这个主题的信息。任何帮助,将不胜感激。谢谢你。
我正在动态创建表格。如果我创建了表Schools和Classes。如何为它们创建模型并指定它们之间的关系。
我搜索但没有得到任何关于这个主题的信息。任何帮助,将不胜感激。谢谢你。
我能弄清楚的唯一方法是对表使用“全局变量” - 例如在 Yii::$app->params['ar_tables'] 中并动态重新定义它们:
在配置中:
[
....
'params' => [
'ar_tables' => [
'Parent' => 'parent',
'Child' => 'table2'
]
]
....
]
父类:
class Parent extends \yii\db\ActiveRecord
{
public static function tableName()
{
return Yii::$app->params['ar_tables']['Parent'];
}
public function getChildren
{
return self::hasMany(Child::className(), ['parent_id' => 'id']);
}
}
儿童班:
class Child extends \yii\db\ActiveRecord
{
public static function tableName()
{
return Yii::$app->params['ar_tables']['Child'];
}
public function getParent
{
return self::hasOne(Parent::className(), ['id' => 'parent_id']);
}
}
之后,您可以动态更改 Yii::$app->params['ar_tables'] 值以获得您想要的。我已经尝试过了。并且不喜欢:)