Problem with relations

I need to get a third-level relationship (I do not know if that’s q is spoken), but I’ll explain.

I have a table Area that is related to the Macropocesso table, Macropocesso table is also related to the Processo table.

But the in the Processo list (index.php) I need to show the column Area.

Is there any way to do this?

Initially I thought using the relationship that already exists between Area and Macroprocesso, since the Processo table is related to Macroprocesso.

My tests are:




<?php

        echo GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

            [

                'attribute' => 'macroprocesso.area.NOME',

                'value' => function($data) {

                    yii\helpers\VarDumper::dump($data, 10, true);

                    //return $data->macroprocesso->area->NOME;

                }

            ],

            [

                'label' => 'Macroprocesso',

                'attribute' => 'MACROPROCESSO_ID',

                'value' => 'macroprocesso.MACROPROCESSO',

            ],






<?php

        echo GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

            [

                'label' => 'Área',

                'value' => 'macroprocesso.area.NOME',

            ],

            [

                'label' => 'Macroprocesso',

                'attribute' => 'MACROPROCESSO_ID',

                'value' => 'macroprocesso.MACROPROCESSO',

            ],



I tried use viaTable() in my relationship, but didn’t work.

Follow my pastebin with 3 models http://pastebin.com/faQ0LzFr

What’s not working in your tests?

With the first block of code, it show me an error PHP Notice – yii\base\ErrorException Trying to get property of non-object. When I dump the object it didn’t show me the Area relation with Processo

On second block of code, it show the column Área empty.

Could it be that area is sometimes null? That would explain the exception.

Hi Patrick.

Area cannot be null.

I noticed a strange behavior. Look.

In index.php widget using


<?php

        echo GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'responsive' => true,

        'hover' => true,

        'resizableColumns' => false,

        'columns' => [

            [

                'attribute' => 'macroprocesso.area.NOME',

                'value' => function($data) {

                   return $data->macroprocesso->area->NOME;

                }

            ],

or


<?php

        echo GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'responsive' => true,

        'hover' => true,

        'resizableColumns' => false,

        'columns' => [

            [

                'attribute' => 'macroprocesso.area.NOME',

                'value' => 'value' => 'macroprocesso.area.NOME',

            ],

It does not work, however in view.php widget it works


<?= DetailView::widget([

        'model' => $model,

        'attributes' => [

            'ID',

            [

                'label' => 'Área',

                'value' => vsprintf('%s - %s', [

                    $model->macroprocesso->area->SIGLA, 

                    $model->macroprocesso->area->NOME]),

            ],

Why does it happens? ? ? ?

It’s completely senseless to me.

How the SAME relation works in a widget and not works in other?

To show what I want, follow my relationship.

7000

relacao-macroprocesso-area.png

So, I want relationship from Processo to Area using an existence relationship among Area and Macroprocesso.

In a Detailview you are looking at ONE record only. In a Gridview you are looking at many. If in any of those "macroprocesso" or "area" is null, you get an exception.

What happens if you use this code? Does it always show the id?


<?php

        echo GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'responsive' => true,

        'hover' => true,

        'resizableColumns' => false,

        'columns' => [

            [

                'macroprocesso.id'

            ],



It return an error Setting unknown property: kartik\grid\DataColumn::0

But I managed to solve the problem. Follow the steps.

Model: ProcessoSearch.php - Método search():


...

$query->joinWith([

            'macroprocesso' => function($join)

            {

                $join->join('LEFT JOIN', 'AUT_SETORES', 

                    'SGR_MACROPROCESSO.AREA_ID = AUT_SETORES.ID');

            },

        ]);

...

In the index.php view follow the steps:


[

    'label' => 'Área',

    'value' => 'macroprocesso.area.SIGLA',

],

I strill have 2 problems:

[list=1]

[*]I can’t use the search field on the widget

[*]I can’t use select clausule in join

[/list]

If you’re not using the standard component, that’s worth mentioning.

Search field for this particular column? If it was the default gridview, this would be your way out:

http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html#working-with-model-relations

Select clause: Do you need a sub-query?

http://www.yiiframework.com/doc-2.0/yii-db-query.html#join()-detail