Yii Framework Forum: Gridview With Foreign Key - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Gridview With Foreign Key

#1 User is offline   venuk 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 15-February 13

Posted 03 December 2013 - 08:16 AM

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?
0

#2 User is offline   Attila B. 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 02-December 13

Posted 03 December 2013 - 02:56 PM

Hello,

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

http://www.yiiframew...hip-is-present/

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

#3 User is offline   Kartik V 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 651
  • Joined: 29-August 12

Posted 18 December 2013 - 04:18 AM

View Postvenuk, on 03 December 2013 - 08:16 AM, said:

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?

Two direct methods probably to do this (though there are many other variations). If you have a model Post and author_id is a foreign key to Author.

Set up the Post model relation:
/**
 * @return \yii\db\ActiveRelation
 */
public function getAuthor() {
    return $this->hasOne(Author::className(), ['author_id' => 'id']);
}


Method 1:
Define a getter method in your Post 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.

Method 2:
Pass the value 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; }]
];

0

#4 User is offline   dannythebestguy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 45
  • Joined: 27-June 12

Posted 04 March 2014 - 03:13 PM

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

Thnx

Dinesh
0

#5 User is offline   Kartik V 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 651
  • Joined: 29-August 12

Posted 05 March 2014 - 08:17 PM

View Postdannythebestguy, on 04 March 2014 - 03:13 PM, said:

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

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.
0

#6 User is offline   anoel 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 22-January 15

Posted 11 February 2015 - 09:05 PM

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users