Trying to get property of non-object

I’m trying to use kartik expandrow. The first level query in PartiesSearch Model is -


$subQuery1 = (new Query())->select(['parties_district','billdate','sum(billamount) as sale'])->from ('parties')->join('LEFT JOIN','bills','bills.bills_partyname = parties.parties_partyname')->groupby('parties_district')->where('billdate != "NULL"');        

        $subQuery2 = (new Query())->select(['district','coalesce(sell.sale,0) as totalsale'])->from('districts')->leftJoin(['sell' => $subQuery1],'sell.parties_district = districts.district'); 

        $subQuery3 = (new Query())->select(['parties_district','payment_date','COALESCE(sum(payment_amount),0) as collection'])->from('payment')->join('LEFT JOIN','parties','payment.payment_partyname = parties.parties_partyname')->groupby('parties_district');

        $query = (new Query())->select(['tsell.district as district','tsell.totalsale as sell','coalesce(tcollection.collection,0) as collection'])->from(['tsell'=> $subQuery2])->leftJoin(['tcollection' => $subQuery3],'tcollection.parties_district = tsell.district');

The second level query in ExpartiesSearch Model is -


$subQuery1 = (new Query())->select(['bills_partyname','billdate','sum(billamount) as sale'])->from('bills')->groupby('bills_partyname');

        $subQuery2 = (new Query())->select(['parties_district','parties_partyname','billdate','coalesce(sell.sale,0) as totalsale'])->from('parties')->leftJoin(['sell' => $subQuery1], 'sell.bills_partyname = parties_partyname')->where('billdate != "NULL"');

        $subQuery3 = (new Query())->select(['payment_partyname','payment_date','COALESCE(sum(payment_amount),0) as collection'])->from('payment')->groupby('payment_partyname');

        $query = (new Query())->select(['tsell.parties_district as district','tsell.parties_partyname as partyname','tsell.billdate as billdate','tsell.totalsale as sell','COALESCE (tcollection.collection,0) as collection'])->from(['tsell'=> $subQuery2])->leftJoin(['tcollection' => $subQuery3],'tcollection.payment_partyname = tsell.parties_partyname');

The code for expandrow in index.php is -


[

            //['class' => 'yii\grid\SerialColumn'],

            'class' => 'kartik\grid\ExpandRowColumn',

            'value' => function($model, $key, $index, $column){

                return GridView::ROW_COLLAPSED;

            },

            'detail' => function($model, $key, $index, $column){

                $searchModel = new ExpartiesSearch();

                $searchModel-> district = $model-> district;

                $dataProvider = $searchModel->search(Yii::$app->request->queryParams);


                return Yii::$app->controller->renderPartial('_exparties', [

                    'searchModel' => $searchModel,

                    'dataProvider' => $dataProvider,

                    ]);                   


                },

            ],

But I’m getting error -


Trying to get property of non-object

The highlighted line is -


$searchModel-> district = $model-> district;

But If I use the following query in the first level t works perfectly -


select 

        tsell.district as district,

        tsell.totalsale as sell,

        coalesce(tcollection.collection,0) as collection 

        from 

            (SELECT 

                district, 

                coalesce(sell.sale,0) as totalsale 

            FROM `districts` 

            left join 

                (SELECT 

                    parties_district, 

                    billdate,

                    sum(billamount) as sale 

                FROM `parties` 

                left join bills on bills.bills_partyname = parties.parties_partyname 

                group by parties_district) as sell 

            on sell.parties_district = districts.district) as tsell 

        left join 

            (SELECT 

                parties_district,

                payment_date,

                COALESCE(sum(payment_amount),0) as collection 

            FROM `payment` 

            left join parties on payment.payment_partyname = parties.parties_partyname 

            group by parties_district) as tcollection 

               on tsell.district = tcollection.parties_district

var_dump($query) shows the following


object(yii\db\Query)#56 (16) { ["select"]=> array(3) { [0]=> string(26) "tsell.district as district" [1]=> string(23) "tsell.totalsale as sell" [2]=> string(48) "coalesce(tcollection.collection,0) as collection" } ["selectOption"]=> NULL ["distinct"]=> NULL ["from"]=> array(1) { ["tsell"]=> object(yii\db\Query)#54 (16) { ["select"]=> array(2) { [0]=> string(<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> "district" [1]=> string(34) "coalesce(sell.sale,0) as totalsale" } ["selectOption"]=> NULL ["distinct"]=> NULL ["from"]=> array(1) { [0]=> string(9) "districts" } ["groupBy"]=> NULL ["join"]=> array(1) { [0]=> array(3) { [0]=> string(9) "LEFT JOIN" [1]=> array(1) { ["sell"]=> object(yii\db\Query)#52 (16) { ["select"]=> array(3) { [0]=> string(16) "parties_district" [1]=> string(<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> "billdate" [2]=> string(23) "sum(billamount) as sale" } ["selectOption"]=> NULL ["distinct"]=> NULL ["from"]=> array(1) { [0]=> string(7) "parties" } ["groupBy"]=> array(1) { [0]=> string(16) "parties_district" } ["join"]=> array(1) { [0]=> array(3) { [0]=> string(9) "LEFT JOIN" [1]=> string(5) "bills" [2]=> string(49) "bills.bills_partyname = parties.parties_partyname" } } ["having"]=> NULL ["union"]=> NULL ["params"]=> array(0) { } ["_events":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> NULL ["where"]=> string(18) "billdate != "NULL"" ["limit"]=> NULL ["offset"]=> NULL ["orderBy"]=> NULL ["indexBy"]=> NULL } } [2]=> string(42) "sell.parties_district = districts.district" } } ["having"]=> NULL ["union"]=> NULL ["params"]=> array(0) { } ["_events":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> NULL ["where"]=> NULL ["limit"]=> NULL ["offset"]=> NULL ["orderBy"]=> NULL ["indexBy"]=> NULL } } ["groupBy"]=> NULL ["join"]=> array(1) { [0]=> array(3) { [0]=> string(9) "LEFT JOIN" [1]=> array(1) { ["tcollection"]=> object(yii\db\Query)#55 (16) { ["select"]=> array(3) { [0]=> string(16) "parties_district" [1]=> string(12) "payment_date" [2]=> string(45) "COALESCE(sum(payment_amount),0) as collection" } ["selectOption"]=> NULL ["distinct"]=> NULL ["from"]=> array(1) { [0]=> string(7) "payment" } ["groupBy"]=> array(1) { [0]=> string(16) "parties_district" } ["join"]=> array(1) { [0]=> array(3) { [0]=> string(9) "LEFT JOIN" [1]=> string(7) "parties" [2]=> string(53) "payment.payment_partyname = parties.parties_partyname" } } ["having"]=> NULL ["union"]=> NULL ["params"]=> array(0) { } ["_events":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> NULL ["where"]=> NULL ["limit"]=> NULL ["offset"]=> NULL ["orderBy"]=> NULL ["indexBy"]=> NULL } } [2]=> string(45) "tcollection.parties_district = tsell.district" } } ["having"]=> NULL ["union"]=> NULL ["params"]=> array(0) { } ["_events":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> NULL ["where"]=> NULL ["limit"]=> NULL ["offset"]=> NULL ["orderBy"]=> NULL ["indexBy"]=> NULL }

Please tell me what is wrong with my original code and what needs to be done to overcome this error.