A
A
Andrey Enkin2016-04-05 02:26:32
Yii
Andrey Enkin, 2016-04-05 02:26:32

Yii2 how to make pagination for grouped rows?

Hello,
I have a database table with events:
screen_1CB32D8A.png
No problem displaying them line by line with pagination using the standard YII tool:

$query = Events::find()
                    ->where(['eventActive'=> 1])
                    ->orderBy('eventDate DESC');

        $count = $query->count();

        $pagination = new Pagination(['totalCount' => $count, 'forcePageParam'=> false, 'defaultPageSize'=>10]);

        $eventsData = $query->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();
        return $this->render('index', [
            'eventsData' => $eventsData,
            'pagination' => $pagination,
        ]);

Example:
Event A
Event B
Event B
Event A
Event B
(in the view it's just a cycle with showing a block)
But I need to group events of the same type that go sequentially, or even better, fall within the date interval:
Event A
2 events B
Event A
Event B
a live example is the display of adding photos in the VK and FB feeds.
How in this case to implement a selection for a specific page? Except for two queries (the first - get all rows with offset, subtract the rows that will be grouped, get a new offset *; the second query with a new offset) has not come up with anything yet, but maybe there is a standard solution?
* after a little thought, I realized that getting a new offset will be recursive, because it must be recalculated again in the event that iterations include events that require grouping.
Therefore, I am clarifying the question: now I am looking for an algorithm and a convenient way to store data for organizing a facebook-like feed with pagination \ lazy load for implementation on Yii2

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
enchikiben, 2016-04-05
@EnChikiben

I did as you described in two queries, you can of course stir up everything with joins and then make the whole thing in code, but it's not worth it (I did it with code, but then another programmer got it for a long time)))).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question