2015-02-23 49 views
0

我對Yii(2)相當陌生。Yii2在gridview中使用自己的sql查詢(gii)

我需要顯示單個產品的總量,忽略位置。如圖所示:

前: enter image description here

後: enter image description here 現在,我需要在GII自動進行GridView控件來顯示這一點。但用我自己的查詢裏面。

因此,不是這樣的: enter image description here 它應該顯示的總金額。

我不知道如何使用我自己的查詢裏面..任何幫助嗎?這是一個標準的Gii CRUD,如果需要可以發佈代碼。

回答

0

您的相關模型中創建新的方法和用$調用數據

<?= 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();} 
     ], 
     .... 
+0

一種更好的方式使用'ActiveRecord':' '值'=>函數($數據){ 回報\程序\車型\ Product :: where(['product_id' - > $ data-> product_id]) - > sum('quantity') - > one();} ],' – 2015-02-23 17:05:46