Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged

Conversation

stefanheimann
Copy link
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