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

#259: Chaperone Tree Relations #260

Merged
merged 24 commits into from
Oct 6, 2024

Conversation

tylernathanreed
Copy link
Contributor

I was recently working on something where I pulled in a tree, and I needed to access information regarding ancestors of the tree. Tapping into these relations caused database queries, despite the models being within the collection already. This resulted in loading 5x the number of models as I had records in the table.

The solution for something like this would be to manually load in the parent/ancestor relations, so that if these relations are called, they load models already in memory, rather than reaching out to the database again.

I've created a new loadTreePathRelations() method, which loads the parent, ancestorsAndSelf, and ancestors relations, which sources as much as possible from the existing collection.

@staudenmeir
Copy link
Owner

Hi @tylernathanreed,
Thanks, I'll take a look.

@staudenmeir
Copy link
Owner

@tylernathanreed
Copy link
Contributor Author

@staudenmeir,

I have added a test.

src/Eloquent/Collection.php Outdated Show resolved Hide resolved
src/Eloquent/Collection.php Outdated Show resolved Hide resolved
tests/Tree/Models/User.php Outdated Show resolved Hide resolved
tests/Tree/Models/User.php Outdated Show resolved Hide resolved
tests/Tree/Models/User.php Outdated Show resolved Hide resolved
src/Eloquent/Collection.php Outdated Show resolved Hide resolved
tests/Tree/CollectionTest.php Outdated Show resolved Hide resolved
tests/Tree/CollectionTest.php Outdated Show resolved Hide resolved
staudenmeir and others added 10 commits October 3, 2024 12:32
Co-authored-by: Sander Muller <github@scode.nl>
Co-authored-by: Sander Muller <github@scode.nl>
Co-authored-by: Sander Muller <github@scode.nl>
Co-authored-by: Sander Muller <github@scode.nl>
Co-authored-by: Sander Muller <github@scode.nl>
Co-authored-by: Sander Muller <github@scode.nl>
@staudenmeir staudenmeir merged commit 6970d6c into staudenmeir:main Oct 6, 2024
41 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.

3 participants