How to combine two pages with two requests into one using Yii2 PHP framework

I am running one query which will return 8 results to me. It's very difficult, so I'm not going to post it here, because maybe it doesn't matter, maybe what I'm trying to do is not possible, but if it is, I hope someone can guide me. So the first query returns 8 results and I will get the ID of each row in the result. I am doing this because in the second query I need to use these identifiers in the WHERE NOT IN clause. I mean the second query will return 18 results because 8 will be ignored since they were found with the first query.

Let me explain further. I had one query that was returning 26 results to me. But the client wanted to order this result by criteria that is not available unless I run the second query and put the results of that query in the results list. I mean, instead of having one query and one list of results, I now have two queries, but their results should be combined into one list where the results of the first query will be returned. But there is a problem with pagination.

Before executing the first query with Yii2 DAO, I create a pagination object that has a totalCount of 26 (I have one counter query that will return me how many results I have). But my first query has 8 results, the second has 18. If I limit the display of results on the page to 10, pagination won't work.

Here's some pseudocode that might help:

// search query that will return number of ids found by certain criteria
$count = $model->countOffers($condition);

// build pagination object
$pagination = new Pagination(['totalCount' => $count, 'pageSize' => 10]);

// execute first query that will return the  array of results
// inside this query $pagination object is used to build LIMIT and OFFSET
$firstQuery = $model->getFirst($condition, $pagination);

// get ID that should be ignored in next query
foreach ($firstQuery as $data) 
{
    $ignoreIds[] = $data['id'];
}

// execute second query that will return results without $ignoredIds being used
// inside this query $pagination object is used to build LIMIT and OFFSET
// $secondQuery = $model->getSecond($condition, $pagination, $ignoredIds);

// render view and display pagination there, but pager is broken
$this->render('index', ['firstQuery' => $firstQuery, 'secondQuery' => $secondQuery, 'pagination' => $pagination]);

      

I am not giving you any specific code because I am not sure if I am trying to do the possible or the impossible. So I would like someone to give me a hint if possible, and if so, what would be the steps to achieve this.

EDIT: This is the index pointer:

<?php if ($queryOne): ?>
    <?= $this->render('_index', ['data' => $queryOne]) ?>
<?php endif ?>

<?php if ($queryTwo): ?>
    <?= $this->render('_index', ['data' => $queryTwo]) ?>
<?php endif ?>

      

+3


source to share


1 answer


Instead of splitting into 2 different results, mark missing articles as "missing" using your complex query in the left join

select if(missing_id is null, 1,0) missing, ...
from articles as a
left join (select id as missing_id ... your-complex-query) 
  as miss on a.id = miss.missing_id

      



So, you will have a single result of 26 rows, 8 with a missing value of 1 and the rest with a missing 0.

0


source







All Articles