Skip to content

[8.x] apply where's from union query builder in cursor pagination#42651

Merged
taylorotwell merged 4 commits intolaravel:8.xfrom
stefanheimann:improvedCursorPaginationWithUnionQuery
Jun 9, 2022
Merged

[8.x] apply where's from union query builder in cursor pagination#42651
taylorotwell merged 4 commits intolaravel:8.xfrom
stefanheimann:improvedCursorPaginationWithUnionQuery

Conversation

@stefanheimann
Copy link
Copy Markdown
Contributor

adds the ability to use cursor pagination with union queries.

here's an example:

$builder->select('id', 'start_time as created_at')->selectRaw("'video' as type")->from('videos');
$builder->union($this->getBuilder()->select('id', 'created_at')->selectRaw("'news' as type")->from('news'));
$builder->orderBy('created_at');

The specified order for this builder instance is now on unionOrders property rather than orders. But further down the road in src/Illuminate/Database/Query/Builder.php method ensureOrderForCursorPagination, the order is retrieved from orders property, which is null. Within that function, the specified order needs to be retrieved either from orders or unionOrders property, it also needs a filter for orders without direction (type "Raw").

Next issue to address is in method paginateUsingCursor from src/Illuminate/Database/Concerns/BuildsQueries.php. It only adds the conditions from the cursor to the main query, not the union query.

So instead of

(select "id", "start_time" as "created_at", 'video' as type from "videos" where ("start_time" > ?)) union (select "id", "created_at", 'news' as type from "news" where ("start_time" > ?)) order by "created_at" asc limit 17

it produces

(select "id", "start_time" as "created_at", 'video' as type from "videos" where ("start_time" > ?)) union (select "id", "created_at", 'news' as type from "news") order by "created_at" asc limit 17

@taylorotwell taylorotwell merged commit 300802f into laravel:8.x Jun 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants