How to pass MySql Custom Query to ActiveDataProvider


(Amjad Alishah312) #1

I am trying to get data using MySql Custom query and pass the result to ActiveDataProvide. Since I Don’t want to send it to ArrayDataProvider. My controller code look like


public function actionIndex()

    {

        $theme = Theme::find();

        $query = "SELECT t.*,COUNT(d.id) AS total_downloads FROM `themes` AS T 

                            LEFT JOIN downloads AS D 

                            ON D.theme_id = T.id GROUP 

                            by t.id ORDER BY total_downloads DESC LIMIT 6";

            $connection=Yii::$app->db;  

            $trends = $connection->createCommand($query);

            $model = $trends->queryAll();

            $object = (object) $model;

        $ActiveDataProvider = new ActiveDataProvider(['query' => $object]);


        return $this->render('trendings',[

                    'ActiveDataProvider'=>$ActiveDataProvider,

                ]);

    }

and view code


<?= 


		ListView::widget([

		    'dataProvider' => $ActiveDataProvider,

		    'options' => [

		        'tag' => 'div',

		        'class' => 'list-wrapper',

		        'id' => 'list-wrapper',

		    ],

		    'pager' => [

		        'firstPageLabel' => 'first',

		        'lastPageLabel' => 'last',

		        'prevPageLabel' => '<span class="glyphicon glyphicon-chevron-left"></span>',

		        'nextPageLabel' => '<span class="glyphicon glyphicon-chevron-right"></span>',

		         'maxButtonCount' => 3,

		    ],

		    // 'layout' => "{pager}\n{items}\n{summary}",

		    'summary' => false,

		    'itemView' => '_list',

		]);

		?>

I am having error in ActiveDataProvider, since ActiveDataProvvider doesn’t QueryAll() so how can i solve this problem?


(Yiiframework) #2

http://www.yiiframework.com/doc-2.0/guide-output-data-providers.html#sql-data-provider ?