Query hints & persistent cache for latest approvals (#5176)

This commit is contained in:
Kevin Papst
2024-11-25 21:04:53 +01:00
committed by GitHub
parent 46c4449504
commit f13b81ede7
11 changed files with 162 additions and 81 deletions

View File

@@ -13,6 +13,9 @@ use App\Entity\Activity;
use App\Entity\Customer;
use App\Entity\Project;
use App\Entity\Timesheet;
use App\Entity\User;
use App\Repository\Query\TimesheetQuery;
use App\Repository\Query\TimesheetQueryHint;
use Doctrine\ORM\EntityManagerInterface;
/**
@@ -23,7 +26,7 @@ final class TimesheetLoader implements LoaderInterface
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly bool $fullyHydrated = false
private readonly ?TimesheetQuery $query = null
)
{
}
@@ -50,7 +53,7 @@ final class TimesheetLoader implements LoaderInterface
return $timesheet->getProject()?->getId();
}, $results)), function ($value) { return $value !== null; });
if ($this->fullyHydrated) {
if ($this->query !== null && $this->query->hasQueryHint(TimesheetQueryHint::PROJECT_META_FIELDS)) {
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL p.{id}', 'meta')
->from(Project::class, 'p')
@@ -60,43 +63,67 @@ final class TimesheetLoader implements LoaderInterface
->execute();
}
$qb = $em->createQueryBuilder();
/** @var array<Project> $projects */
$projects = $qb->select('PARTIAL p.{id}', 'customer')
->from(Project::class, 'p')
->leftJoin('p.customer', 'customer')
->andWhere($qb->expr()->in('p.id', $projectIds))
->getQuery()
->execute();
if ($this->fullyHydrated) {
$customerIds = array_filter(array_unique(array_map(function (Project $project) {
return $project->getCustomer()?->getId();
}, $projects)), function ($value) { return $value !== null; });
if (\count($projectIds) > 0) {
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL c.{id}', 'meta')
->from(Customer::class, 'c')
->leftJoin('c.meta', 'meta')
->andWhere($qb->expr()->in('c.id', $customerIds))
/** @var array<Project> $projects */
$projects = $qb->select('PARTIAL p.{id}', 'customer')
->from(Project::class, 'p')
->leftJoin('p.customer', 'customer')
->andWhere($qb->expr()->in('p.id', $projectIds))
->getQuery()
->execute();
if ($this->query !== null && $this->query->hasQueryHint(TimesheetQueryHint::CUSTOMER_META_FIELDS)) {
$customerIds = array_filter(array_unique(array_map(function (Project $project) {
return $project->getCustomer()?->getId();
}, $projects)), function ($value) { return $value !== null; });
if (\count($customerIds) > 0) {
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL c.{id}', 'meta')
->from(Customer::class, 'c')
->leftJoin('c.meta', 'meta')
->andWhere($qb->expr()->in('c.id', $customerIds))
->getQuery()
->execute();
}
}
}
if ($this->fullyHydrated) {
if ($this->query !== null && $this->query->hasQueryHint(TimesheetQueryHint::ACTIVITY_META_FIELDS)) {
$activityIds = array_filter(array_map(function (Timesheet $timesheet) {
return $timesheet->getActivity()?->getId();
}, $results), function ($id): bool {
return $id !== null;
});
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL a.{id}', 'meta')
->from(Activity::class, 'a')
->leftJoin('a.meta', 'meta')
->andWhere($qb->expr()->in('a.id', $activityIds))
->getQuery()
->execute();
if (\count($activityIds) > 0) {
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL a.{id}', 'meta')
->from(Activity::class, 'a')
->leftJoin('a.meta', 'meta')
->andWhere($qb->expr()->in('a.id', $activityIds))
->getQuery()
->execute();
}
}
if ($this->query !== null && $this->query->hasQueryHint(TimesheetQueryHint::USER_PREFERENCES)) {
$userIds = array_filter(array_map(function (Timesheet $timesheet) {
return $timesheet->getUser()?->getId();
}, $results), function ($id): bool {
return $id !== null;
});
if (\count($userIds) > 0) {
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL u.{id}', 'preferences')
->from(User::class, 'u')
->leftJoin('u.preferences', 'preferences')
->andWhere($qb->expr()->in('u.id', $userIds))
->getQuery()
->execute();
}
}
$qb = $em->createQueryBuilder();