0
votes

I have this

 public function actionIndex()
{
    $data = order::getSome();
    $dataProvider = new ArrayDataProvider([
        'allModels' => $data,
        'sort' => [
            'attributes' => ['paid']
        ]


    ]);
    return $this->render('index',['dataProvider' =>$dataProvider]);
}

If in gridview delete 'columns'=> [, there is post all columns in data correct. If in 'columns'=> [ put names of columns, there is '0' in all columns. How to display specific columns?

`public static function getSome(){
     return   Yii::$app->getDb()->createCommand(
            "SELECT order_customFields.order_customFields_delivery_method,

    sum(case `order`.order_status when 'paid' then 1 else 0 end) paid,
    sum(case `order`.order_status when 'later' then 1 else 0 end) later,
  sum(case `order`.order_status when 'delivery-approved' then 1 else 0 end) deliveryapproved,

FROM order_customFields 
  INNER JOIN `order` ON order_customFields.order_id = `order`.order_id
WHERE
  order_customFields.order_customFields_order_date >= '2016-12-01' AND
  order_customFields.order_customFields_order_date <= '2016-12-31' 
AND order_customFields.order_customFields_delivery_method is not null
GROUP BY    
   order_customFields.order_customFields_delivery_method"
        )->queryAll();
    }`
2
Explain breiflyYasar Arafath
Please include the code that isn't working.topher

2 Answers

1
votes

based on your sample could be you are using an activeRecord Order (and not order)

 Order::getSome();

this function give you the related models so why you are not use the canonical way for yii2 like

/**
 * Lists all AntigoneResidente models.
 * @return mixed
 */
public function actionIndex()
{
    $searchModel = new OrderSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);


    return $this->render('@common/views/antigone-residente/index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}

then in gridview you can access to specific column

<?= GridView::widget([
      'dataProvider' => $dataProvider,
      'filterModel' => $searchModel,
      'columns' => [
          ['class' => 'yii\grid\SerialColumn'],
          //'id',
          'your_column_name1',
          'your_column_name2',
          .....

          ['class' => 'yii\grid\ActionColumn',],
      ],
]); ?>
0
votes

If you want to use arrayDataProvider then you have to pass data in array format not in object

     public function actionIndex()
    {
        $data = order::getSome()->asArray();
        $dataProvider = new ArrayDataProvider([
            'key'=>'id',
            'allModels' => $data,
            'sort' => [
                'attributes' => ['id','paid','vendorid','orderno'],
            ],
    ]);

}

In your Gridview

    echo GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            'id',
            'paid', 
            'vendorid',
'orderno'
            ],

    ]
]);