Add field in a Gii form from another table

I have a table called child (id, name, sport_id), a table called sports (id, name), a table called parent (id, name) and a table called child_parent(id, child_id, parent_id).

I created the crud with Gii and I modified the child_parent to show not the id of the child and parent but the names, also made a dropdownlist for search and for the create form. But one thing I can’t do is put an extra column on the index from called ‘Sport’, so I can filter childs/parents by sport. How can I do this?

do you have some code parts that can give more information of what you mean? i might know what you want but i can’t really explain without some code that gives extra information.

Ok, here’s my controller:


 public function actionIndex()

    {

        $query = ChildParent::find();

        $searchModel = new ChildParentSearch();


        if(isset($_GET['ChildParentSearch']))

        {

            $searchModel->load(Yii::$app->request->get());

            $query->joinWith(['child']);

            $query->joinWith(['parent']);

            $query->andFilterWhere(['LIKE', 'child.id', $searchModel->getAttribute('child_id')]);

            $query->andFilterWhere(['LIKE', 'parent.id', $searchModel->getAttribute('parent_id')]);

        }


        $dataProvider = new ActiveDataProvider([

            'query' => $query,

            'pagination' => [

                'pageSize' => 50

            ]

        ]);

The child table is (id, name, sport_id), so I want to add a column in my view to search childs for sport too. Here’s my view:


$childFilterData = \yii\helpers\ArrayHelper::map(\common\models\Child::find()->all(),'id','name');

$parentFilterData = \yii\helpers\ArrayHelper::map(\common\models\Parent::find()->all(), 'id', 'name');


?>

<div class="child-parent-index">


    <h1><?= Html::encode($this->title) ?></h1>

    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>


    <p>

        <?= Html::a('Create', ['create'], ['class' => 'btn btn-success']) ?>

    </p>


    <?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],

            [

                'header' => 'Child',

                'attribute' => 'child.name',

                'filter' => Html::activeDropDownList($searchModel, 'child_id', $childFilterData, ['prompt' => '-- All', 'class' => 'form-control']), 'content' => function($model) { return $model->child->name; }

            ],

            [

                'header' => 'Parent',

                'attribute' => 'parent.name',

                'filter' => Html::activeDropDownList($searchModel, 'parent_id', $parentFilterData, ['prompt' => '-- All', 'class' => 'form-control']), 'content' => function($model) { return $model->parent->name; }

            ],


            ['class' => 'yii\grid\ActionColumn'],

        ],

    ]); ?>


</div>