-
-
Notifications
You must be signed in to change notification settings - Fork 84
feat: Add the Relations Bulk Loader #539
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## 2.x #539 +/- ##
============================================
+ Coverage 89.93% 91.42% +1.49%
- Complexity 1892 1925 +33
============================================
Files 120 123 +3
Lines 4860 4947 +87
============================================
+ Hits 4371 4523 +152
+ Misses 489 424 -65 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
6493745 to
e9945a8
Compare
fix tests names; add test for RefersTo
4683752 to
a20127f
Compare
06a9542 to
bb6cbb5
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🔍 What was changed
Added relations bulk loader to load relations
📝 Checklist
📃 Documentation: Bulk Loading Relations
When you fetch multiple entities and need to load their relations, doing it one by one causes the N+1 query problem.
BulkLoader solves this by loading relations for all entities at once.
Only unloaded relations are resolved - already loaded relations won't be overwritten.
Getting BulkLoader Instance
To load relations, use the
\Cycle\ORM\Relation\BulkLoaderInterface.If you're using a Cycle ORM bridge, get the BulkLoader instance from the DI container:
If you're not using a bridge, configure the binding in your application container:
Then inject it into your services as shown above.
Basic Usage
The
BulkLoaderInterface::collect()method is immutable and returns a new instanceof
Cycle\ORM\Relation\RelationLoaderInterfacethat you can use to chainload()calls and finally executerun().This executes just 2 queries instead of 1 + N: one to fetch users, one to fetch all their profiles.
Multiple and Nested Relations
Important Behavior
All entities must be of the same role.
Entities must be loaded from the database (not new).
Relations use database state, not runtime changes. It's required because to ensure consistency.
Already loaded relations are not overwritten: