From e1f8aa5a5ef3c6c75087f4e5589213598b5cddc2 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Wed, 30 Jun 2021 15:49:23 +0200 Subject: [PATCH] Use custom Doctrine hydrator. --- .../Controller/SocialIssueApiController.php | 40 ++----------------- 1 file changed, 4 insertions(+), 36 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Controller/SocialIssueApiController.php b/src/Bundle/ChillPersonBundle/Controller/SocialIssueApiController.php index 4437955ed..63deadd03 100644 --- a/src/Bundle/ChillPersonBundle/Controller/SocialIssueApiController.php +++ b/src/Bundle/ChillPersonBundle/Controller/SocialIssueApiController.php @@ -3,11 +3,9 @@ namespace Chill\PersonBundle\Controller; use Chill\MainBundle\CRUD\Controller\ApiController; +use Chill\MainBundle\Doctrine\ORM\Hydration\FlatHierarchyEntityHydrator; use Chill\MainBundle\Pagination\PaginatorInterface; -use Chill\PersonBundle\Entity\SocialWork\SocialIssue; use DateTimeImmutable; -use Doctrine\ORM\QueryBuilder; -use Generator; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -26,23 +24,9 @@ class SocialIssueApiController extends ApiController protected function getQueryResult(string $action, Request $request, string $_format, int $totalItems, PaginatorInterface $paginator, $query) { - // Create a lazy generator to avoid performance issues. - $generator = $this->buildRecursively( - $query->getQuery()->getResult(), - static fn (SocialIssue $socialIssue): iterable => $socialIssue->getChildren() - ); - - // Sadly, we must convert the generator into an array... - // so we lose all the performance gain we could have by using an iterator. - $results = []; - - // Reduce the generator into an array containing once each SocialIssue. - foreach ($generator as $socialIssue) { - // If the current item hasn't been seen yet, add it to the resultset. - $results += [$socialIssue->getId() => $socialIssue]; - } - - return $results; + // In order to work, this hydrator only works with + // entities having the field "children" set up. + return $query->getQuery()->getResult(FlatHierarchyEntityHydrator::LIST); } protected function onPostIndexBuildQuery(string $action, Request $request, string $_format, int $totalItems, PaginatorInterface $paginator, $query): ?Response @@ -53,20 +37,4 @@ class SocialIssueApiController extends ApiController return null; } - - /** - * @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); - } - } }