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

[4.x] Order by primary key when using lazy() or chunk() #9956

Merged

Conversation

helloiamlukas
Copy link
Contributor

When using lazy() or chunk() within the EloquentQueryBuilder, the result could change in unexpected ways because it is not sorted.

The Laravel documentation recommends sorting by id when using the chunk/lazy function. In Laravel this is done through a seperate chunkById/lazyById function.

I don't know if Statamic should offer 1.) a seperate chunkById/lazyById (like the Laravel query builder) or 2.) the chunk/lazy function should already implement this behaviour by default.

I would argue that you don't ever want a situation where the results are not ordered by the primary key, therefore I implemented the second approach within this pull request.

Similar to statamic/eloquent-driver#232

@helloiamlukas helloiamlukas changed the title Order by primary key when using lazy() or chunk() [4.x] Order by primary key when using lazy() or chunk() Apr 24, 2024
@jasonvarga
Copy link
Member

Thank you, but you said it yourself that Laravel tells you to either manually order by id, or use lazyById.

I think with what you've done, if you wanted to order by something else, your change would override their order.

We can add those lazyById/chunkById methods.

@jasonvarga jasonvarga closed this Apr 24, 2024
@helloiamlukas
Copy link
Contributor Author

Should I open another pull request then?

After lazyById and chunkById are implemented: Would you just replace the current uses of chunk and lazy? Or would you override these uses within the Eloquent driver?

For example here:

return $query->lazy(100)->filter($this->filter())->values();

@jasonvarga
Copy link
Member

jasonvarga commented Apr 24, 2024

Yes, open another PR if you'd like.

In that example we'd update it in core to use lazyById

@helloiamlukas
Copy link
Contributor Author

helloiamlukas commented Apr 25, 2024

Ok, I looked into the Laravel query builder code now and they actually add the sorting by the primary key by default, but only if no other orderBy are set.

https://github.com/laravel/framework/blob/c2668c7deeabb59c96181b8a6e2bb2c08b9425eb/src/Illuminate/Database/Concerns/BuildsQueries.php#L32-L35

https://github.com/laravel/framework/blob/c2668c7deeabb59c96181b8a6e2bb2c08b9425eb/src/Illuminate/Database/Eloquent/Builder.php#L908-L918

This would actually solve the current problems, without adding lazyById and chunkById.

I pushed the changes to this branch, do you want to re-open this pr?

@jasonvarga
Copy link
Member

Good catch. Yes, that'll be fine.

@jasonvarga jasonvarga reopened this Apr 25, 2024
@jasonvarga jasonvarga merged commit cd88092 into statamic:4.x May 8, 2024
36 checks passed
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.

None yet

2 participants