Problem with GridView Call to a member function getCount() on a non-object

Hello I am new to yii and trying for a few days now to create a gridview either with yii2 gridview or with kartik gridview with no success.

Today I decided to try yii2 GridView again.

When running my page I get this error:

Call to a member function getCount() on a non-object

My model code:


namespace app\models;


use yii;

use yii\data\SqlDataProvider;


class InvoiceDiscountDataProvider extends \yii\data\ArrayDataProvider

{

    public static function getInvoicesTotals($company_id, $report_type)

    {

        $count = Yii::$app->db->createCommand('

            SELECT count(invoice_discount_report__id)

            FROM tbl_invoice_discount_report

            WHERE invoice_discount_report__subscriber_company__id = :company_id  

            AND invoice_discount_report__report_type = :report_type

            GROUP BY invoice_discount_report__debtor__name',

            [':company_id' => $company_id, 'report_type' => $report_type])->queryScalar();


        $provider = new SqlDataProvider([

            'sql' => 'SELECT invoice_discount_report__debtor__name AS customer,

                    COUNT(invoice_discount_report__invoice_number) AS invoices,

                    SUM(invoice_discount_report__invoice_amount) AS amount,

                    SUM(invoice_discount_report__payment_amount) AS amount_paid,

                    SUM(invoice_discount_report__discount_amount) AS discount

                    FROM tbl_invoice_discount_report

                    WHERE invoice_discount_report__subscriber_company__id = :company_id  

                    AND invoice_discount_report__report_type = :report_type ',

            'params' => [':company_id' => $company_id, 'report_type' => $report_type],

            'totalCount' => $count,

            'pagination' => [

                'pageSize' => 10,

            ],

            'sort' => [

                'attributes' => [

                    'customer'

                ],

            ],

        ]);


        // returns an array of data rows

        $models = $provider->getModels();

    

        return $models;

    }

My controller code:


$data_provider = InvoiceDiscountDataProvider::getInvoicesTotals($company_id, $report_type);

        

        return $this->render('dashboard', ['data_provider'=>$data_provider]);

My view code:




<?= GridView::widget([

                'dataProvider' => $data_provider,

                'columns' => [

                    'Customer',

                    'Invoices',

                    'Amount',

                    'Amount Paid',

                    'Discount'

                ],

            ]); ?>



What am I doing wrong?

Please help, thank you.

why you don’t use activerecord?

I tried, got all kinds of errors with it. I don’t know how to do the sql statement that I need with ActiveRecord.

I think you’re passing models instead of any kind of DataProvider class. Try to add your models into an ArrayDataProvider and pass it to the GridView