perform search on another properties from master table

Hi Everyone,

how to perform searching on another field (properties) from its master table.

example: I have purchasing table and agen table. purchasing table have field called ‘agen_id’ reference to table agen (agen_id column). while table agen have another column called agen_name and sub_agen.

in index.php (belong to purchasing table) file, i have configured like this:




$gridColumns = [

        ['class' => 'kartik\grid\SerialColumn'],

        [

            'class' => 'kartik\grid\ActionColumn',

            'dropdown' => false,

            'vAlign'=>'middle',            

        ],        

        'trx_id',

        'buy_date',

        'vehicle_id',

        [

            'attribute' => 'agen_id',

            'value' => 'agen.agen_name'

        ],

        'agen.sub_agen'

];



What i want is to search ‘agen.sub_agen’ too.

What must i do? Please advice. Thanks before

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

Thanks a lot.

and I have another question is about to calculate fields between relation table.

let’s say i have model A and reference to model B.

in index.php for model A, i need to calculate column table A called ‘netto’ multiply with column model B called ‘fee_agen’

ilustrated code




[

            'class'=> 'kartik\grid\FormulaColumn', 

            'header' => 'Total Fee',

            'value' => function($model){

                return ($B->fee_agen * $model->netto);

            },

            'format'=>['decimal', 0],

            'hAlign' => 'right'

],



how to perform formula like this in gridView?

many thanks.

You mean how to do that in a standard GridView? Same way, you define "value":

http://www.yiiframework.com/doc-2.0/yii-grid-datacolumn.html#$value-detail

Alternatively you can implement a custom attribute.

I’m using kartik GridView. I can’t called $agen->fee_agen to multiply with $model->netto.

do you mean like this ?




[

            'class'=> 'kartik\grid\FormulaColumn', 

            'header' => 'Subtotal Fee Agen',

            'value' => function($model, $column){

                return ($model->r_bersih * $column['fee_agen']);

            },

            'format'=>['decimal', 0],

            'hAlign' => 'right'

],



If there’s a relation between A and B:




            'value' => function($model){

                return ($model->B->fee_agen * $model->netto);

            },



Or you can implement a "getTotal" in your model and then use "total" as the column in the grid:

http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#selecting-extra-fields

http://www.yiiframework.com/doc-2.0/guide-concept-properties.html

Hi Patrick Jones,

You help me a lot. Many thanks. It works with $model->B->attributes.

Regards,

Wilson