refactored repositories and DB queries (#5026)

* removed unused teams from export order
* added new paginator for query instead of querybuilder
* added field hydrate enums
* hide PARTIAL deprecation
* never log deprecations in production
* replaced InvoiceLoader with native Doctrine feature
* prevent excessive permission queries
* support loading customers of team
* improved findByIds
* internalized API
* fix null string deprecations
This commit is contained in:
Kevin Papst
2024-08-27 10:11:19 +02:00
committed by GitHub
parent 9e3d243b4b
commit 9d933f62c0
81 changed files with 1648 additions and 1436 deletions

View File

@@ -11,15 +11,19 @@ namespace App\Repository\Result;
use App\Entity\Timesheet;
use App\Repository\Loader\TimesheetLoader;
use App\Repository\Paginator\LoaderPaginator;
use App\Repository\Paginator\LoaderQueryPaginator;
use App\Repository\Query\TimesheetQuery;
use App\Utils\Pagination;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
/**
* @internal
*/
final class TimesheetResult
{
private ?TimesheetResultStatistic $statisticCache = null;
private bool $cachedFullyHydrated = false;
/**
* @var array<Timesheet>|null
*/
@@ -27,16 +31,22 @@ final class TimesheetResult
/**
* @internal
* @param Query<null, Timesheet> $query
*/
public function __construct(private TimesheetQuery $query, private QueryBuilder $queryBuilder)
public function __construct(
private readonly TimesheetQuery $timesheetQuery,
private readonly EntityManagerInterface $entityManager,
private readonly QueryBuilder $statisticQb,
private readonly Query $query
)
{
}
public function getStatistic(): TimesheetResultStatistic
{
if ($this->statisticCache === null) {
$withDuration = $this->query->countFilter() > 0;
$qb = clone $this->queryBuilder;
$withDuration = $this->timesheetQuery->countFilter() > 0;
$qb = clone $this->statisticQb;
$qb
->resetDQLPart('select')
->resetDQLPart('orderBy')
@@ -47,6 +57,7 @@ final class TimesheetResult
$qb->addSelect('COALESCE(SUM(t.duration), 0) as duration');
}
/** @var array{'duration': int<0, max>, 'counter': int<0, max>} $result */
$result = $qb->getQuery()->getArrayResult()[0];
$duration = $withDuration ? $result['duration'] : 0;
@@ -58,39 +69,34 @@ final class TimesheetResult
public function toIterable(): iterable
{
$query = $this->queryBuilder->getQuery();
return $query->toIterable();
return $this->query->toIterable();
}
/**
* @param bool $fullyHydrated
* @return array<Timesheet>
*/
public function getResults(bool $fullyHydrated = false): array
public function getResults(): array
{
if ($this->resultCache === null || ($fullyHydrated && $this->cachedFullyHydrated === false)) {
$query = $this->queryBuilder->getQuery();
$results = $query->getResult();
if ($this->resultCache === null) {
/** @var array<Timesheet> $results */
$results = $this->query->getResult();
$loader = new TimesheetLoader($this->queryBuilder->getEntityManager(), $fullyHydrated);
$loader = new TimesheetLoader($this->entityManager, true);
$loader->loadResults($results);
$this->cachedFullyHydrated = $fullyHydrated;
$this->resultCache = $results;
}
return $this->resultCache;
}
public function getPagerfanta(bool $fullyHydrated = false): Pagination
public function getPagerfanta(): Pagination
{
$qb = clone $this->queryBuilder;
$loader = new LoaderQueryPaginator(new TimesheetLoader($this->entityManager), $this->query, $this->getStatistic()->getCount());
$loader = new LoaderPaginator(new TimesheetLoader($qb->getEntityManager(), $fullyHydrated), $qb, $this->getStatistic()->getCount());
$paginator = new Pagination($loader);
$paginator->setMaxPerPage($this->query->getPageSize());
$paginator->setCurrentPage($this->query->getPage());
$paginator->setMaxPerPage($this->timesheetQuery->getPageSize());
$paginator->setCurrentPage($this->timesheetQuery->getPage());
return $paginator;
}