I have 3 Tables:
CREATE TABLE tabCve
(
intCveID INTEGER NOT NULL AUTO_INCREMENT,
strNumber VARCHAR(20) NOT NULL,
fltScore FLOAT(0),
strDescriptio
visit a complete tutorial about this question in :
http://www.yiiframework.com/wiki/851/yii2-gridview-sorting-and-searching-with-a-junction-table-column-many-to-many-relationship/
according to this tutorial you should : 1. set your related table attribute as public in search model like this :
public $groupname;
2 . include attribute in the rules :
public function rules() {
return [
[['id', 'gender', 'status', 'sentstatus'], 'integer'],
[['groupname', 'addeddate', 'updateddate'], 'safe'],
];
}
change default query to following code in function search()
in search model :
public function search($params) { $query = Contacts::find()->innerJoinWith('groups', true);
and in andfilterwhere add wanted attribute . like this :
$query->andFilterWhere(['like', 'firstname', $this->firstname]) ... ->andFilterWhere(['like', 'groupname', $this->groupname]);
in your view class and in Gridview your wanted column should like this :
'columns' => [
...
[
'attribute' => 'tags',
'format' => 'raw',
'value' => function ($data) {
$groups= '';
foreach ($data->groups as $group) {
$groups .= '' .
$group->group_name . ' | ';
}
return $groups;
},
'filter' => ArrayHelper::map(group::find()->asArray()->all(), 'group_name','group_name'),
],
and if table relation is hasOne()
you should read this tutorial :
http://www.yiiframework.com/wiki/653/displaying-sorting-and-filtering-model-relations-on-a-gridview/