Joining two tables and showing the contents in a CGrigView

If I want to join two or more tables and show the result in a CGrigView how would I do this?

If "a" is your main model and "rel" is your related model:


echo GridView::widget([

    'dataProvider' => $dataProvider,

    'columns' => [

        'field_from_model_a',

        'rel.field_from_model_rel',

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

    ],

]);

http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html

I have same problem, but i cant resolve as you said. I used gii to create models, controllers and crud.

I did this on Search model:


public function search($params) {

	$query = Localidades::find ()->innerJoin ( 'Provincias', 'Localidades.IdProvincia = Provincias.IdProvincia' )->select ( 'Provincias.Denominacion AS Provincia, Localidades.Denominacion AS Localidad' );

		

	$dataProvider = new ActiveDataProvider ( [ 'query' => $query ] );


	if (! ($this->load ( $params ) && $this->validate ())) {

		return $dataProvider;

	}

		

	$query->andFilterWhere ( [ 

		'IdLocalidad' => $this->IdLocalidad,

		'IdProvincia' => $this->IdProvincia 

	] );

		

	$query->andFilterWhere ( [ 

		'like',

		'Localidad',

		$this->Denominacion 

	] );

		

	return $dataProvider;

}

But when i try to print this query on GridView from Index model:


<?=GridView::widget ( [ 

	    'dataProvider' => $dataProvider,

	    'filterModel' => $searchModel,

	    'columns' => [ 

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

	    	'Localidad',

	    	'Provincia',

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

	    ]

] );?>

Widget doesnt recognize them:

6172

yii problem.jpg

I used to do it easy without using Yii, but now this simple thing is taking my time, almost 2 days with this problem.

Also read link you post but i cant do it, so i ask for some help. Thanks.-

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