Multiple related rows in gridview kartik

Hello, I’m new using yii2, I’m learning too much about this incredible framework.

I’m working with kartik gridview and I need to show related data by row, not using implode or join but I can’t do it yet.

my tables:
Person:
id_person pk
id_degree fk
lu fk
name
lastname

Important: id_degree & lu = 1 person

academics
id_academic
id_degree fk
lu fk
cod_uni_academic
cod_title

analytical:
id_analytical
id_degree fk
lu fk
subject

Important: 1 person -> many subjects

So this is my model Person::

public function getAcademic()
{
    return $this->hasOne(Academics::className(), ['id_carrera' => 'id_carrera', 'lu' => 'lu']);
}

public function getCod_uni_academica()
{
    return $this->academics->cod_uni_academic;
}

public function getCod_title()
{
    return $this->academics->cod_title;
}
.
.
.... more functions




public function getAnalytical()
    {
        return $this->hasMany(Analytical::className(), ['id_degree => 'id_degree', 'lu' => 'lu']);
    }

my view index.php

$analiticos = GridView::widget([
            'id' => 'gridAnaliticos',
            'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,
            'pjax' => true,
            'options' => [ 'style' => 'table-layout: fixed;'],
            'headerRowOptions' => ['class' => 'kartik-sheet-style'],
            'filterRowOptions' => ['class' => 'kartik-sheet-style'],
            'columns' => [
                [
                    'class' => '\kartik\grid\CheckboxColumn',
                    'headerOptions' => ['class' => 'kartik-sheet-style'],
                ],
                'name',
                'lastname',
                [
                    'attribute'=>'subjects',
                    'headerOptions' => ['style' => 'width:30%'],
                    'format' => 'html',
                    'value' => function ($model) {
                        $output = '';
                        foreach($model->analytical as $row) {
                            $output .= $row->subject."<br />";
                        }
                        return $output;
                    },
                ],

Now, I show in datagridview all rows concatenated like this image:


But I need show subject by row, something like that:

This is posible?

Thank you in advance

Dear galazzaroni

  1. It is a bad idea to use foreach in column value function. Go OOP way.

  2. You should have code for every column you need in the GridView.

Regards.

Thank you @Jay_69 for reply.

Do you say that I have to do a function in the model for each value that I have to show in the grid?
I updated main post.

I did that with the academic table that has a one-to-one relationship.
But I do not know how to do it with the analytics that a person has many subjects for example.

could you give me an example?
Remember that I want to show row by row, no concatenated like I show in the pic.

thank you so much.

Dear galazzaroni,

I have never used kartik GridView, but to me it seems you should use \kartik\grid\ExpandRowColumn and be able to use master-detail feature to show one-to-many related data.

Regards.