GridView and Foreign Key

Hi,

I have a table “procedimiento” who has a foreign key to table “organizacion”. I have generated CRUD code with gii. I got a GridView working fine for table “procedimiento”. I’m able to show “organizacion” fields using this code (see “$data->organizacion0->nombre” reference):




    <?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

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


            'id',

            'cod_sia:ntext',

            'nombre:ntext',

            [

                'header' => 'Responsable',

                'value' => function ($data) {

                    return $data->organizacion0->nombre;

                }

            ],

            'comentario:ntext',


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

        ],

    ]); ?> 

but I can not search in this column. I found some links with useful information for Yii 1.1 but nothing for Yii 2.

You should include


$query->andFilterWhere(<your_filter_rule>)

in public method "search" (procedimientoSearch model).

Antonio

Thanks Antonio. Finally I found this link:

http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/#hh10

and Search is working… but not sorting. I continue to investigate.