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;
|
namespace Chill\PersonBundle\Controller;
|
||||||
|
|
||||||
use Chill\MainBundle\CRUD\Controller\ApiController;
|
use Chill\MainBundle\CRUD\Controller\ApiController;
|
||||||
|
use Chill\MainBundle\Doctrine\ORM\Hydration\FlatHierarchyEntityHydrator;
|
||||||
use Chill\MainBundle\Pagination\PaginatorInterface;
|
use Chill\MainBundle\Pagination\PaginatorInterface;
|
||||||
use Chill\PersonBundle\Entity\SocialWork\SocialIssue;
|
|
||||||
use DateTimeImmutable;
|
use DateTimeImmutable;
|
||||||
use Doctrine\ORM\QueryBuilder;
|
|
||||||
use Generator;
|
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
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)
|
protected function getQueryResult(string $action, Request $request, string $_format, int $totalItems, PaginatorInterface $paginator, $query)
|
||||||
{
|
{
|
||||||
// Create a lazy generator to avoid performance issues.
|
// In order to work, this hydrator only works with
|
||||||
$generator = $this->buildRecursively(
|
// entities having the field "children" set up.
|
||||||
$query->getQuery()->getResult(),
|
return $query->getQuery()->getResult(FlatHierarchyEntityHydrator::LIST);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function onPostIndexBuildQuery(string $action, Request $request, string $_format, int $totalItems, PaginatorInterface $paginator, $query): ?Response
|
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;
|
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