Query - Multiple tables

I’ve the following tables.

student


id

name

score


id

studentID

subjectID

marks

subject


id

name

In view.php, I want to display the following

subject.name | score.marks

My code in StudentController.php :

public function actionView($id)

{


$scoreDataProvider = new ActiveDataProvider([


  


		  'query' => Score::find(),


				'pagination' => [


				'pageSize' => 10,


			],


		]);


return $this->render('view',[


		'model'=>$this->findModel($id),


		'scoreDataProvider'=>$scoreDataProvider,





	]);


}

However, the query did not return any results. Please help.

Additional Info.

In Student Model

public function getScores()


{


    return $this->hasMany(Score::className(), ['studentID' => 'id']);


}

In Score Model

public function getStudent()


{


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


}








public function getSubject()


{


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


}

In Subject Model

public function getScore()


{


    return $this->hasMany(Score::className(), ['subjectID' => 'id']);


}

The strange thing is that the query did not return any results,

because if there are records in Score they should be displayed.

Anyway, data provider should be:




$scoreDataProvider = new ActiveDataProvider([


'query' => Score::find()->with('subject'),

'pagination' => [

'pageSize' => 10,

],

]);



So you will be able to call ‘subject.name’ in the GridView or ListView.

Can you post your view content?

Fabrizio, thanks for the quick response. Still "No results found"

<?= GridView::widget([

    'dataProvider' =&gt; &#036;scoreDataProvider,


    'columns' =&gt; [


        'subject.name',


    


    ],

]) ?>

Are you sure that $dataProvider is filled?

Try with:




var_dump($scoreDataProvider->getModels());



to see if contains data.

Yes. The $dataProvider is filled. Thanks for the var dump method.

My problem is the same as http://stackoverflow.com/questions/30772270/yii2-column-in-gridview-is-not-displaying-not-set

but I don’t understand the solution.

You should see ‘not set’ value for that column, if there are records in dataProvider.

Thanks Fabrizio.

In view.php, I want to display the following

subject.name | score.marks

Using var_dump($scoreDataProvider->getModels());- I was able to identify the fields that’s available.

Instead of ‘subject.name’ and ‘score.marks’, I’ll just have to display ‘name’ and ‘marks’.