wilsonim
(Wilson Im)
September 11, 2016, 11:39am
1
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
patrickm
(Yiiframework)
September 11, 2016, 4:32pm
2
wilsonim
(Wilson Im)
September 12, 2016, 12:18am
3
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.
patrickm
(Yiiframework)
September 12, 2016, 6:42am
4
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.
wilsonim
(Wilson Im)
September 12, 2016, 8:57am
5
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'
],
patrickm
(Yiiframework)
September 12, 2016, 1:01pm
6
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
wilsonim
(Wilson Im)
September 12, 2016, 2:13pm
7
Hi Patrick Jones,
You help me a lot. Many thanks. It works with $model->B->attributes.
Regards,
Wilson