YII2 Activedataprovider custom template

2019-03-02 09:56发布

问题:

Let's assume that we have three tables, tbl_student ,tbl_lesson and tbl_score(student_id, lesson_id, score).

for Activedataprovider, we have:

$query = \app\models\Score::find()
    ->joinWith('student', false)
    ->joinWith('lesson', false);

$provider = new \yii\data\ActiveDataProvider([
    'query' => $array,
]);

echo GridView::widget([
    'dataProvider' => $dataProvider,
]);

So it gives an output with columns : student_id, lesson_id, score:

<table border="1">
  <tr>
    <td> student_id </td>
    <td> lesson_id </td>
    <td> score </td>  
  </tr>
  
  <tr>
    <td> 1 </td>
    <td> 1 </td>
    <td> 99.5 </td>
  </tr>
  
  <tr>
    <td> 1 </td>
    <td> 2 </td>
    <td> 54 </td>
  </tr>
  
  <tr>
    <td> 1 </td>
    <td> 3 </td>
    <td> 87 </td>
  </tr>
  
  <tr>
    <td> 2 </td>
    <td> 1 </td>
    <td> 76 </td>
  </tr>
  
  <tr>
    <td> 2 </td>
    <td> 2 </td>
    <td> 84 </td>
  </tr>
  
  <tr>
    <td> 2 </td>
    <td> 3 </td>
    <td> 69 </td>
  </tr>
  
</table>

But what I want is to display students in the first COLUMN, and lessons in the first ROW and then display the associated scores in the body of the table:

<table border="1">
  <tr>
    <td>student_id</td>
    <td>lesson_1</td>
    <td>lesson_2</td>
    <td>lesson_3</td>
  </tr>

  <tr>
    <td>1</td>
    <td>99.5</td>
    <td>54</td>
    <td>87</td>
  </tr>

  <tr>
    <td>2</td>
    <td>76</td>
    <td>84</td>
    <td>69</td>
  </tr>
</table>

How can I do that? Thanks in advance.


I know that I can use Arraydataprovider, but as said in Yii2 data providers guide

Note: Compared to Active Data Provider and SQL Data Provider, array data provider is less efficient because it requires loading all data into the memory.


What I want:

but I want to do it using Activedataprovider

标签: yii2