Yii Framework Forum: How to pass MySql Custom Query to ActiveDataProvider - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to pass MySql Custom Query to ActiveDataProvider Rate Topic: -----

#1 User is offline   Amjad Ali Shah 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 09-November 15

Posted 10 May 2017 - 04:00 AM

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?
0

#2 User is offline   Patrick Jones 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 496
  • Joined: 14-July 12
  • Location:Berlin

Posted 10 May 2017 - 05:55 AM

http://www.yiiframew...l-data-provider ?
Freelance developer for hire - send me a message!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users