diff --git a/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php b/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php index 6826f854c..1637ee098 100644 --- a/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php +++ b/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php @@ -32,9 +32,8 @@ use Chill\MainBundle\Doctrine\DQL\JsonAggregate; use Chill\MainBundle\Doctrine\DQL\JsonbExistsInArray; use Chill\MainBundle\Doctrine\DQL\Similarity; use Chill\MainBundle\Doctrine\DQL\OverlapsI; -use Symfony\Component\DependencyInjection\Definition; -use Symfony\Component\DependencyInjection\Reference; use Chill\MainBundle\Doctrine\DQL\Replace; +use Chill\MainBundle\Doctrine\ORM\Hydration\FlatHierarchyEntityHydrator; use Chill\MainBundle\Doctrine\Type\NativeDateIntervalType; use Chill\MainBundle\Doctrine\Type\PointType; use Symfony\Component\HttpFoundation\Request; @@ -186,6 +185,9 @@ class ChillMainExtension extends Extension implements PrependExtensionInterface, 'OVERLAPSI' => OverlapsI::class, ], ], + 'hydrators' => [ + 'chill_flat_hierarchy_list' => FlatHierarchyEntityHydrator::class, + ], ], ], ); diff --git a/src/Bundle/ChillMainBundle/Doctrine/ORM/Hydration/FlatHierarchyEntityHydrator.php b/src/Bundle/ChillMainBundle/Doctrine/ORM/Hydration/FlatHierarchyEntityHydrator.php new file mode 100644 index 000000000..ed7588f94 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Doctrine/ORM/Hydration/FlatHierarchyEntityHydrator.php @@ -0,0 +1,48 @@ +buildRecursively( + parent::hydrateAllData(), + static fn($value): iterable => $value->getChildren(), + ); + + $result = []; + + foreach ($generator as $entity) { + // We cannot expect anything from the object entity + // so, we cannot expect it to have a ::getId() method. + // So we use spl_object_hash() instead. + $result += [spl_object_hash($entity) => $entity]; + } + + return array_values($result); + } + + /** + * @param iterable $iterable + * @param callable(SocialIssue): iterable $childrenAccessor + * + * @return Generator + */ + private function buildRecursively(iterable $iterable, callable $childrenAccessor): Generator + { + foreach ($iterable as $item) + { + yield $item; + + yield from $this->buildRecursively($childrenAccessor($item), $childrenAccessor); + } + } +}