Hello,
I have 3 tables, let’s say: student, course and coursestudent, the last is a junction table. So a student could participate on many courses and a course can be attended by multiple students.
I need to show multiple attributes from the course in the student view page, using a GridView and in a row per course fashion. For example, if I go to the view page of student John Doe I need to display every course that he’s participating in, using attributes from the course table:
John Doe’s view page
Course Name | Starting Date | Hours -> attributes from the class table
course1 xx-xx-xxx 80
course2 xx-xx-xxx 75 -> every row is a class where studentX is enrolled
I do not need sorting and searching for this view, also I removed the serial and action parts of the GridView.
So far I was able to get the needed data on the Grid, but is displayed like an array in one row, instead of a row per value.
This is what I have done:
<?php
$query = Student::find()
->joinWith('courseperstudents')
->where(['courseperstudent.course_id' => $model->id_student])
;
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => false,
]) ?>
And in the widget:
<?= GridView::widget([
'dataProvider' => $dataProvider,
//'summary' => '',
'columns' => [
[
'label' => 'Course',
'value' => function($model) {
return join(', ', yii\helpers\ArrayHelper::map($model->courseperstudents, 'id_courseperstudent', 'course.course_name'));
},
],
//other columns needed using the same logic
...],
]); ?>