在另一个 table 的 Gii 表单中添加字段

Add field in a Gii form from another table

我有一个table叫child(id,name,sport_id),一个table叫sports(id,name),一个table 称为 parent (id, name) 和一个 table 称为 child_parent(id, child_id, parent_id).

我用 Gii 创建了 crud,我修改了 child_parent 以显示名称而不是 child 和 parent 的 ID,还制作了一个用于搜索和查找的下拉列表创建表单。但是我不能做的一件事是在名为 'Sport' 的索引上添加一个额外的列,这样我就可以按运动过滤 childs/parents。我怎样才能做到这一点?

Junction table不是必需的,你可以使用自引用关系。

  • 节点(id,parent_id,sport_id,名称)
  • 运动(身份证、姓名)

parent_id 默认为 NULL(root)。如果需要,您可以为字段添加规则或条件可见性,强制子项与父项具有相同的 sport_id(除非它是根)。

现在您可以轻松启用按运动筛选。

[
    'attribute' => 'sport_id',
    'filter' => Html::activeDropDownList(
        $searchModel, 
        'sport_id',
        ArrayHelper::map(Sport::find()->all(),'id','name'),
        ['prompt' => '']
    ),
    'value' => function($model) {
        return $model->sport->name;
    }
],