Showing relational data in GridView

Dear all

I’m struggling with this easy task for a long time now… I have a table fotocategorie with a foreign key foto_idfoto to another table called foto (with PK idfoto).

What I want is a GridView, where instead of the key as an int, I want the name of foto (attribute titel).

But I does not seem to work. I did the following:

In FotoCategorieSearch I added:




    /**

     * Foto linked to this category

     */

    public $fotocategorie_foto;



and




    public function rules() {

        return [

            [['idFotoCategorie', 'foto_idfoto'], 'integer'],

            [['titel', 'fotocategorie_foto'], 'safe'],

        ];

    }



No I updated the search (with a join)




$query= FotoCategorie::find()->joinWith('fotoIdfoto');



My Fotocategorie has this:




    /**

     * @return \yii\db\ActiveQuery

     */

    public function getFotoIdfoto()

    {

        return $this->hasOne(Foto::className(), ['idfoto' => 'foto_idfoto']);

    }



and a filer:




andFilterWhere(['like', 'foto.titel', $this->fotocategorie_foto]);



In the view I add:




            <?=

            GridView::widget([

                'dataProvider' => $dataProvider,

                'filterModel' => $searchModel,

                'columns' => [

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

                    'idFotoCategorie',

                    'titel',

                    [


                        'attribute' => 'fotocategorie_foto',

                        'label' => 'Foto',

                        'value' => 'foto.titel',

                    ],

                    'titel:ntext',

                    'foto_idfoto',

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

                ],

            ]);

            ?>



But this does not seem to work. What am I doing wrong? Exception is: "Getting unknown property: app\models\Fotocategorie::foto"

Thanks in advance!

Check this wiki article, it should help you understand how this works - http://www.yiiframework.com/wiki/653/displaying-sorting-and-filtering-model-relations-on-a-gridview

Dear Maurizo

This is the acutal tutorial I followed. It seems this is not working. I guess the join operation is not performing, but is there any way to check this?

Kind regards

Jens Buysse