Paramone
Paramone

Reputation: 2724

Yii2 use own sql query in gridview (gii)

I'm fairly new to Yii(2).

I need to show the total amount of a single product, ignoring the locations. As shown here:

Before: enter image description here

After: enter image description here Now, I need to show this in the gridview that Gii automatically made. But with my own query inside of it.

So instead of this: enter image description here It should show the total amount.

I have no idea how to use my own query inside of that.. Any help please? It's a standard Gii CRUD, can post code if requested.

Upvotes: 0

Views: 1131

Answers (1)

sambua
sambua

Reputation: 2634

Create new method inside of your related model and call with $data

 <?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        .....
        # below $data is current Model initial
        ['label' => 'Count', 'format' => 'html',
          'value' => function($data){return $data::getCreatedStaticFuntion($data->product_id);}
        ],
        # or use like below
        ['label' => 'Count', 'format' => 'html',
          'value' => function($data){
            $sql = 'SELECT * FROM tbl_product WHERE id ='.data->product_id;
            return \app\models\Product::findBySql($sql)->all();}
        ],
        ....

Upvotes: 0

Related Questions