Gridview With Foreign Key

In Yii2, What is the best way to display foreign key value instead of id in Gridview?

Any simple way of getting the value of id?

Hello,

Actually, this piece is answered in my question on this forum: :lol:

http://www.yiiframework.com/forum/index.php/topic/49447-how-to-use-filters-in-the-gridview-widget-when-table-relationship-is-present/

I’m still not sure how to make the filter work though.

Two direct methods probably to do this (though there are many other variations). If you have a model [font="Courier New"]Post[/font] and [font="Courier New"]author_id[/font] is a foreign key to [font="Courier New"]Author[/font].

Set up the [font="Courier New"]Post[/font] model relation:




/**

 * @return \yii\db\ActiveRelation

 */

public function getAuthor() {

    return $this->hasOne(Author::className(), ['author_id' => 'id']);

}



[size="3"]Method 1:[/size]

Define a getter method in your [font="Courier New"]Post[/font] model. If you need related name in a lot of places, this maybe useful.




public function getAuthorName() {

    return $this->author->name;

}



Call this in your gridview:




'columns' => [

    'id',

    ['label'=>'Author', 'attribute'=>'authorName']

];



Note: Filtering of data will not be possible for the above approach. If this is needed shift to method 2.

[size="3"]Method 2:[/size]

Pass the [font="Courier New"]value[/font] to your grid data column as a Closure. Very direct approach and specific to the grid.




'columns' => [

    'id',

    ['label'=>'Author', 'value'=>function ($model, $index, $widget) { return $model->author->name; }]

];



Kartik

On the method 2 how do I do the filter with the name. It does the filtering with the id.

Thnx

Dinesh

1 Like

Option 2a: You need to setup the data column correctly. Set the ‘attribute’ value to the database field (in this example author_id). You can filter this column through author id.




'columns' => [

    ['attribute'=>'author_id', 'label'=>'Author', 'value'=>function ($model, $index, $widget) { return $model->author->name; }]

];



Option 2b: However, filtering by author id may not be user friendly. You would like to filter by Author Name. You can set the filter to be a dropdown list.




$authorList = ArrayHelper::map(Author::find()->asArray->all(), 'id', 'name');

//

'columns' => [

    ['attribute'=>'author_id', 'filter'=>$authorList, 'label'=>'Author', 'value'=>function ($model, $index, $widget) { return $model->author->name; }]

];



NOTE: in both the options above, sorting will happen by author id.

Option 2c: However if filtering by dropdown list is also not desired and you need a free text search (with sorting by foreign key), you would need to use the approach as described in my wiki.

Hi,

I’ve got a problem with displaying a foreign key value in a gridview…


PHP Warning – yii\base\ErrorException


htmlspecialchars() expects parameter 1 to be string, object given

As I’m a newbie with yii I can’t figure out what am I missing. I have two tables Controllo{id, articolo, …} and Articoli{id, codice_articolo} where attribute articolo is a foreign key to Articoli(id). I’ve generated the model for both of them.

In Controllo.php i have also this method.


public function getArticolo(){

return $this->hasOne(Articoli::className(), ['id' => 'articolo']);

}

and in the view


<?= DetailView::widget([


'model' => $model,


'attributes' => [


'id',


['attribute'=>'articolo', 'label'=>'Aticolo', 


'value'=>function ($model, $index, $widget) { return $model->articolo->codice_articolo; }],


...

],


]) ?>

Hope somebody can help me to make this work