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,58 +11,73 @@ namespace App\Repository\Loader;
use App\Entity\Customer;
use App\Entity\Team;
use App\Repository\Query\CustomerQuery;
use App\Repository\Query\CustomerQueryHydrate;
use Doctrine\ORM\EntityManagerInterface;
/**
* @internal
* @implements LoaderInterface<Customer>
*/
final class CustomerLoader implements LoaderInterface
{
public function __construct(private EntityManagerInterface $entityManager, private bool $fullyHydrated = false)
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly CustomerQuery $query
)
{
}
/**
* @param array<int|Customer> $results
* @param array<Customer> $results
*/
public function loadResults(array $results): void
{
if (empty($results)) {
if (\count($results) === 0) {
return;
}
$ids = array_map(function ($customer) {
if ($customer instanceof Customer) {
// make sure that this potential doctrine proxy is initialized and filled with all data
$customer->getName();
$customerIds = array_filter(array_unique(array_map(function (Customer $customer) {
// make sure that this potential doctrine proxy is initialized and filled with all data
$customer->getName();
return $customer->getId();
return $customer->getId();
}, $results)), function ($value) { return $value !== null; });
$hydrateTeams = false;
$hydrateTeamMembers = false;
foreach ($this->query->getHydrate() as $hydrate) {
switch ($hydrate) {
case CustomerQueryHydrate::TEAMS:
$hydrateTeams = true;
break;
case CustomerQueryHydrate::TEAM_MEMBER:
$hydrateTeams = true;
$hydrateTeamMembers = true;
break;
}
}
return $customer;
}, $results);
if (!$hydrateTeams) {
return;
}
$em = $this->entityManager;
$qb = $em->createQueryBuilder();
/** @var Customer[] $customers */
$customers = $qb->select('PARTIAL c.{id}', 'meta')
->from(Customer::class, 'c')
->leftJoin('c.meta', 'meta')
->andWhere($qb->expr()->in('c.id', $ids))
->getQuery()
->execute();
// required where we need to check team permissions, e.g. "Customer listing"
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL c.{id}', 'teams')
->from(Customer::class, 'c')
->leftJoin('c.teams', 'teams')
->andWhere($qb->expr()->in('c.id', $ids))
->andWhere($qb->expr()->in('c.id', $customerIds))
->getQuery()
->execute();
// do not load team members or leads by default, because they will only be used on detail pages
// and there is no benefit in adding multiple queries for most requests when they are only needed in one place
if ($this->fullyHydrated) {
if ($hydrateTeamMembers) {
$teamIds = [];
foreach ($customers as $customer) {
foreach ($results as $customer) {
foreach ($customer->getTeams() as $team) {
$teamIds[] = $team->getId();
}