Borah Anurup
Borah Anurup

Reputation: 43

Yii2 Pagination Issue on Union

I am trying to use Pagination after union of two queries the pagination does not seems to work. However if I try to make one queries without union it works. The below are the queries.Please help.

//First Query
$first_second = $this->find()->select($strcolumn.', p.featured_name')->from(MYDIRECTORY::tableName().' j')->join('INNER JOIN' ,MYDIRECTORYFEATUREDCLASS::tableName().' p', 'p.featurer_id = j.listdetails_featured_frid')->where(['listdetails_list_frid' => $id['list_id']])->andWhere(['<=', 'listdetails_featured_frid', 2])->andWhere(['listdetails_list_flag' => MYDIRECTORYCLASS::STATUS_ACTIVE])->orderBy(['listdetails_featured_frid'=>SORT_ASC,'listdetails_list_pos'=>new Expression('rand()')]);

 //Second Query
$second_list =  $this->find()->select($strcolumn.', p.featured_name')->from(MYDIRECTORYCLASS::tableName().' j')->join('INNER JOIN' ,MYDIRECTORYFEATUREDCLASS::tableName().' p', 'p.featurer_id = j.listdetails_featured_frid')->where(['listdetails_list_frid' => $id['list_id']])->andWhere(['>', 'listdetails_featured_frid', 2])->andWhere(['listdetails_list_flag' => MYDIRECTORYCLASS::STATUS_ACTIVE])->orderBy(['listdetails_featured_frid'=>SORT_ASC,'listdetails_list_medname'=>SORT_ASC]);


//Joined Union Query
$joinedquerys=$first_second->union($second_list);

$countQuery = clone $joinedquerys;

$pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => \Yii::$app->params['pagination_limit'],'defaultPageSize' => \Yii::$app->params['pagination_limit'],'forcePageParam' => false,'params' => ['page' => \Yii::$app->request->get('page', 1)] ]);

$resultArray = $joinedquerys->offset($pages->offset)->limit($pages->limit)->asArray()->all();


return $this->render('listing', [
            "mylisting" =>  $resultArray,
            "pagination" => $pages
]);

While using the pagination as below , the pagination seems to not work?Any help will be greatly appreciated

echo LinkPager::widget([
                    'pagination' => $pagination,
                    'options' => ['class' => 'paginate pag2 clearfix'],
                    'registerLinkTags' => true,
                    'prevPageLabel' => \YII::$app->params['linker_page_btn_prev'],
                    'nextPageLabel' => \YII::$app->params['linker_page_btn_next'],
                    'maxButtonCount' => \YII::$app->params['linker_page_btn_count'],
                    'activePageCssClass' => 'current',
                     'nextPageCssClass' => 'next'
]);

Upvotes: 2

Views: 767

Answers (2)

mrateb
mrateb

Reputation: 2499

I had the same problem, but in addition I had to use the ActiveDataProvider. To achieve this I had to do the following:

    $joinedQuery = $first_second->union($second_list);
    $dirQuery = MYDIRECTORY::find()->from(['directories' => $joinedQuery]);
    $provider = new ActiveDataProvider([
            'query' => $dirQuery,
        ]); 

Hope this helps someone :)

Upvotes: 2

ScaisEdge
ScaisEdge

Reputation: 133370

you could use dataProvider

<?php


  $joinedquerys=$first_second->union($second_list);



  $dataProvider = new SqlDataProvider([
    'sql' => $joinedquerys,

  ]);

?>

Upvotes: 2

Related Questions