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

[6.x] Improve augmentation of relationships #435

Merged
merged 2 commits into from
Feb 16, 2024

Conversation

duncanmcclean
Copy link
Member

@duncanmcclean duncanmcclean commented Feb 16, 2024

This pull request refactors how Eloquent Relationships are augmented to improve performance and prevent as many duplicate queries, like seen in #430.

Changes include:

  • Instead of a HasMany/BelongsTo relationship instance being passed to the augmentation methods, we now pass an Eloquent\Collection instance.
    • Essentially, we're now doing $model->relationship instead of $model->relationship(). This change also lets us take advantage of eager loading for the related models.
  • I've refactored the augmentation methods in the BaseFieldtype to simplify it, now it only needs to accept an Eloquent\Collection or an array of IDs (in the case the fieldtype is being used on entries).
  • The runwayUri relationship no longer gets augmented.
    • There's no reason for the runwayUri relation to be augmented in the first place so it's a quick win 🚀
  • Previously, the {{ runway }} tag wasn't eager loading any relationships, unless you were providing the relationships to be eager loaded yourself.
    • Now, similar to the listing table in the CP, Runway will automatically eager load models based on the relationship fields you have configured in your resource blueprint.

Testing

On my local sandbox, I managed to reduce the number of total queries for index pages (which use the {{ runway }} tag) down from a couple hundred to 3 queries.

Then, in the demo site provided in #430, I've managed to get rid of the duplicate queries & reduce the total number of queries down to 108, which is more reasonable than a couple thousand.


Closes #430.

@duncanmcclean duncanmcclean changed the title [6.x] Refactor relationship augmentation [6.x] Improve augmentation of relationships Feb 16, 2024
@duncanmcclean duncanmcclean marked this pull request as ready for review February 16, 2024 13:02
@duncanmcclean duncanmcclean merged commit 282d3dd into 6.x Feb 16, 2024
7 checks passed
@duncanmcclean duncanmcclean deleted the refactor-relationship-augmentation branch February 16, 2024 13:02
Copy link

Released as part of v6.2.0.

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.

Augmentation N+1 query issue on frontend
1 participant