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
Merged
Changes from 2 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
69e2ba9
#259: Added method to load in relations.
tylernathanreed Sep 5, 2024
f8b6ce0
#259: Fixed typo
tylernathanreed Sep 5, 2024
29c4de4
#259: Added missing include
tylernathanreed Sep 5, 2024
d1090c5
#259: Added attributes to be used with test
tylernathanreed Sep 6, 2024
75dac50
#259: Updated to match schema
tylernathanreed Sep 6, 2024
5c0ba5c
#259: Added initial test
tylernathanreed Sep 6, 2024
5ab4447
#259: Updated attribute names
tylernathanreed Sep 6, 2024
f3d30bb
#259: Updated to use display path
tylernathanreed Sep 6, 2024
386c156
#259: Added missing include
tylernathanreed Sep 6, 2024
33d19db
#259: Sort results to ensure consistency across database platforms
tylernathanreed Sep 6, 2024
3a87803
#259: More sorting to ensure consistency across database platforms
tylernathanreed Sep 6, 2024
5c96ad2
Merge branch 'main' into features/259-chaperone
staudenmeir Sep 7, 2024
d22eaa4
Merge branch 'main' into features/259-chaperone
staudenmeir Sep 7, 2024
0afd951
Merge branch 'main' into features/259-chaperone
staudenmeir Oct 3, 2024
ea6e47d
Update src/Eloquent/Collection.php
staudenmeir Oct 6, 2024
6ffb174
Update src/Eloquent/Collection.php
staudenmeir Oct 6, 2024
ce2e061
Update tests/Tree/Models/User.php
staudenmeir Oct 6, 2024
4605096
Update tests/Tree/Models/User.php
staudenmeir Oct 6, 2024
896782f
Update src/Eloquent/Collection.php
staudenmeir Oct 6, 2024
ffad79c
Update tests/Tree/CollectionTest.php
staudenmeir Oct 6, 2024
171d66c
Merge branch 'main' into features/259-chaperone
staudenmeir Oct 6, 2024
d310743
Fix loading of missing models
staudenmeir Oct 6, 2024
531b6e8
Refactoring
staudenmeir Oct 6, 2024
5f98c34
Refactoring
staudenmeir Oct 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,59 @@

return $tree;
}

/**
* Load parent/anscestor relations already present in the tree.
*
* @return static<int, TModel>
*/
public function loadTreePathRelations()
staudenmeir marked this conversation as resolved.
Show resolved Hide resolved
{
$instance = $this->first();

if (is_null($instance)) {
return $this;
}

if (! method_exists($instance, 'getPathName') || ! method_exists($instance, 'getPathSeparator')) {
throw new RuntimeException(sprintf(

Check failure on line 67 in src/Eloquent/Collection.php

View workflow job for this annotation

GitHub Actions / phpstan (8.3, stable)

Instantiated class Staudenmeir\LaravelAdjacencyList\Eloquent\RuntimeException not found.
tylernathanreed marked this conversation as resolved.
Show resolved Hide resolved
'Model [%s] does not have recusive relations.',
staudenmeir marked this conversation as resolved.
Show resolved Hide resolved
$instance::class,
));
}

$keyName = $instance->getKeyName();
$pathName = $instance->getPathName();
$pathSeparator = $instance->getPathSeparator();

$lookup = $this->keyBy($keyName);

$keys = $this
->pluck($pathName)
->flatMap(fn (string $path): array => explode($pathSeparator, $path))
->unique()
->values();

$missing = $keys->diff($lookup->modelKeys());

if ($missing->isNotEmpty()) {
$lookup->merge($instance->newQuery()->findMany($missing)->keyBy($keyName));
}

foreach ($this->all() as $model) {
$path = array_reverse(explode($pathSeparator, $model->getAttribute($pathName)));

$ancestorsAndSelf = array_reduce(
$path,
fn ($collection, $step) => $collection->push($lookup[$step] ?? null),
$instance->newCollection(),
);

$model->setRelation('parent', count($path) > 1 ? $lookup[$path[1]] : null);
$model->setRelation('ancestorsAndSelf', $ancestorsAndSelf);
$model->setRelation('ancestors', $ancestorsAndSelf->slice(0, -1));
staudenmeir marked this conversation as resolved.
Show resolved Hide resolved
staudenmeir marked this conversation as resolved.
Show resolved Hide resolved
}

return $this;
}
}
Loading