mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Use custom Doctrine hydrator.
This commit is contained in:
parent
d208a79764
commit
e1f8aa5a5e
@ -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<int, SocialIssue> $iterable
|
||||
* @param callable(SocialIssue): iterable<int, SocialIssue> $childrenAccessor
|
||||
*
|
||||
* @return Generator<int, SocialIssue>
|
||||
*/
|
||||
private function buildRecursively(iterable $iterable, callable $childrenAccessor): Generator
|
||||
{
|
||||
foreach ($iterable as $item)
|
||||
{
|
||||
yield $item;
|
||||
|
||||
yield from $this->buildRecursively($childrenAccessor($item), $childrenAccessor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user