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

@@ -12,65 +12,44 @@ namespace App\Repository\Loader;
use App\Entity\Activity;
use App\Entity\Customer;
use App\Entity\Project;
use App\Entity\Team;
use Doctrine\ORM\EntityManagerInterface;
/**
* @internal
* @implements LoaderInterface<Activity>
*/
final class ActivityLoader implements LoaderInterface
{
public function __construct(private EntityManagerInterface $entityManager, private bool $fullyHydrated = false)
public function __construct(private readonly EntityManagerInterface $entityManager)
{
}
/**
* @param array<int|Activity> $results
* @param array<Activity> $results
*/
public function loadResults(array $results): void
{
if (empty($results)) {
if (\count($results) === 0) {
return;
}
$ids = array_map(function ($activity) {
if ($activity instanceof Activity) {
// make sure that this potential doctrine proxy is initialized and filled with all data
$activity->getName();
$activityIds = array_filter(array_unique(array_map(function (Activity $activity) {
// make sure that this potential doctrine proxy is initialized and filled with all data
$activity->getName();
// using reporting controller tests will show that error
$activity->getProject()?->getName();
return $activity->getId();
}
return $activity;
}, $results);
return $activity->getId();
}, $results)), function ($value) { return $value !== null; });
$em = $this->entityManager;
$qb = $em->createQueryBuilder();
/** @var Activity[] $activities */
$activities = $qb->select('PARTIAL a.{id}', 'project')
->from(Activity::class, 'a')
->leftJoin('a.project', 'project')
->andWhere($qb->expr()->isNotNull('a.project'))
->andWhere($qb->expr()->in('a.id', $ids))
->getQuery()
->execute();
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL a.{id}', 'meta')
->from(Activity::class, 'a')
->leftJoin('a.meta', 'meta')
->andWhere($qb->expr()->in('a.id', $ids))
->getQuery()
->execute();
$projectIds = array_filter(array_unique(array_map(function (Activity $activity) {
return $activity->getProject()?->getId();
}, $results)), function ($value) { return $value !== null; });
// global activities don't have projects
if (!empty($activities)) {
$projectIds = array_unique(array_map(function (Activity $activity) {
if (null === $activity->getProject()) {
return null;
}
return $activity->getProject()->getId();
}, $activities));
if (\count($projectIds) > 0) {
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL project.{id}', 'customer')
->from(Project::class, 'project')
@@ -79,14 +58,6 @@ final class ActivityLoader implements LoaderInterface
->getQuery()
->execute();
$customerIds = array_unique(array_map(function (Activity $activity) {
if (null === $activity->getProject()) {
return null;
}
return $activity->getProject()->getCustomer()->getId();
}, $activities));
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL project.{id}', 'teams')
->from(Project::class, 'project')
@@ -95,44 +66,28 @@ final class ActivityLoader implements LoaderInterface
->getQuery()
->execute();
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL customer.{id}', 'teams')
->from(Customer::class, 'customer')
->leftJoin('customer.teams', 'teams')
->andWhere($qb->expr()->in('customer.id', $customerIds))
->getQuery()
->execute();
}
$customerIds = array_filter(array_unique(array_map(function (Activity $activity) {
return $activity->getProject()?->getCustomer()?->getId();
}, $results)), function ($value) { return $value !== null; });
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL a.{id}', 'teams')
->from(Activity::class, 'a')
->leftJoin('a.teams', 'teams')
->andWhere($qb->expr()->in('a.id', $ids))
->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) {
$teamIds = [];
foreach ($activities as $activity) {
foreach ($activity->getTeams() as $team) {
$teamIds[] = $team->getId();
}
}
$teamIds = array_unique($teamIds);
if (\count($teamIds) > 0) {
if (\count($customerIds) > 0) {
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL team.{id}', 'members', 'user')
->from(Team::class, 'team')
->leftJoin('team.members', 'members')
->leftJoin('members.user', 'user')
->andWhere($qb->expr()->in('team.id', $teamIds))
$qb->select('PARTIAL customer.{id}', 'teams')
->from(Customer::class, 'customer')
->leftJoin('customer.teams', 'teams')
->andWhere($qb->expr()->in('customer.id', $customerIds))
->getQuery()
->execute();
}
}
// required on "Activity listing" page for non super-admins
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL a.{id}', 'teams')
->from(Activity::class, 'a')
->leftJoin('a.teams', 'teams')
->andWhere($qb->expr()->in('a.id', $activityIds))
->getQuery()
->execute();
}
}

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();
}

View File

@@ -9,6 +9,10 @@
namespace App\Repository\Loader;
/**
* @deprecated use QueryBuilderPaginator instead
* @implements LoaderInterface<mixed>
*/
final class DefaultLoader implements LoaderInterface
{
public function loadResults(array $results): void

View File

@@ -1,65 +0,0 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Repository\Loader;
use App\Entity\Invoice;
use Doctrine\ORM\EntityManagerInterface;
final class InvoiceLoader implements LoaderInterface
{
public function __construct(private EntityManagerInterface $entityManager)
{
}
/**
* @param array<int|Invoice> $results
*/
public function loadResults(array $results): void
{
if (empty($results)) {
return;
}
$ids = array_map(function ($invoice) {
if ($invoice instanceof Invoice) {
// make sure that this potential doctrine proxy is initialized and filled with all data
$invoice->getInvoiceNumber();
return $invoice->getId();
}
return $invoice;
}, $results);
$em = $this->entityManager;
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL i.{id}', 'customer')
->from(Invoice::class, 'i')
->leftJoin('i.customer', 'customer')
->getQuery()
->execute();
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL i.{id}', 'user')
->from(Invoice::class, 'i')
->leftJoin('i.user', 'user')
->getQuery()
->execute();
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL i.{id}', 'meta')
->from(Invoice::class, 'i')
->leftJoin('i.meta', 'meta')
->andWhere($qb->expr()->in('i.id', $ids))
->getQuery()
->execute();
}
}

View File

@@ -9,12 +9,15 @@
namespace App\Repository\Loader;
/**
* @template T
*/
interface LoaderInterface
{
/**
* Prepares the given database results, to prevent lazy loading.
*
* @param array $results
* @param array<array-key, T> $results
*/
public function loadResults(array $results): void;
}

View File

@@ -14,63 +14,50 @@ use App\Entity\Project;
use App\Entity\Team;
use Doctrine\ORM\EntityManagerInterface;
/**
* @internal
* @implements LoaderInterface<Project>
*/
final class ProjectLoader implements LoaderInterface
{
public function __construct(private EntityManagerInterface $entityManager, private bool $hydrateTeamMembers = false, private bool $hydrateTeams = true, private bool $hydrateMeta = true)
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly bool $hydrateTeamMembers = false,
private readonly bool $hydrateTeams = true
)
{
}
/**
* @param array<int|Project> $results
* @param array<Project> $results
*/
public function loadResults(array $results): void
{
if (empty($results)) {
if (\count($results) === 0) {
return;
}
$ids = array_map(function ($project) {
if ($project instanceof Project) {
// make sure that this potential doctrine proxy is initialized and filled with all data
$project->getName();
$projectIds = array_filter(array_unique(array_map(function (Project $project) {
// make sure that this potential doctrine proxy is initialized and filled with all data
$project->getName();
// using reporting controller tests will show that error
$project->getCustomer()?->getName();
return $project->getId();
}
return $project;
}, $results);
return $project->getId();
}, $results)), function ($value) { return $value !== null; });
$em = $this->entityManager;
$qb = $em->createQueryBuilder();
/** @var Project[] $projects */
$projects = $qb->select('PARTIAL project.{id}', 'customer')
->from(Project::class, 'project')
->leftJoin('project.customer', 'customer')
->andWhere($qb->expr()->in('project.id', $ids))
->getQuery()
->execute();
$customerIds = array_unique(array_map(function (Project $project) {
return $project->getCustomer()->getId();
}, $projects));
if ($this->hydrateMeta) {
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL project.{id}', 'meta')
->from(Project::class, 'project')
->leftJoin('project.meta', 'meta')
->andWhere($qb->expr()->in('project.id', $ids))
->getQuery()
->execute();
}
if ($this->hydrateTeams) {
$customerIds = array_filter(array_unique(array_map(function (Project $project) {
return $project->getCustomer()->getId();
}, $results)), function ($value) { return $value !== null; });
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL project.{id}', 'teams')
->from(Project::class, 'project')
->leftJoin('project.teams', 'teams')
->andWhere($qb->expr()->in('project.id', $ids))
->andWhere($qb->expr()->in('project.id', $projectIds))
->getQuery()
->execute();
@@ -87,7 +74,7 @@ final class ProjectLoader implements LoaderInterface
// and there is no benefit in adding multiple queries for most requests when they are only needed in one place
if ($this->hydrateTeamMembers) {
$teamIds = [];
foreach ($projects as $project) {
foreach ($results as $project) {
foreach ($project->getTeams() as $team) {
$teamIds[] = $team->getId();
}

View File

@@ -9,52 +9,77 @@
namespace App\Repository\Loader;
use App\Entity\Project;
use App\Entity\Team;
use Doctrine\ORM\EntityManagerInterface;
/**
* @internal
* @implements LoaderInterface<Team>
*/
final class TeamLoader implements LoaderInterface
{
public function __construct(private EntityManagerInterface $entityManager)
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly bool $loadCustomer = false
)
{
}
/**
* @param array<int|Team> $results
* @param array<Team> $results
*/
public function loadResults(array $results): void
{
if (empty($results)) {
if (\count($results) === 0) {
return;
}
$ids = array_map(function ($team) {
if ($team instanceof Team) {
// make sure that this potential doctrine proxy is initialized and filled with all data
$team->getName();
$teamIds = array_filter(array_unique(array_map(function (Team $team) {
// make sure that this potential doctrine proxy is initialized and filled with all data
$team->getName();
return $team->getId();
}
return $team;
}, $results);
return $team->getId();
}, $results)), function ($value) { return $value !== null; });
$em = $this->entityManager;
// required wherever users are shown, e.g. on "Custom details" page
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL team.{id}', 'members', 'user')
->from(Team::class, 'team')
->leftJoin('team.members', 'members')
->leftJoin('members.user', 'user')
->andWhere($qb->expr()->in('team.id', $ids))
->andWhere($qb->expr()->in('team.id', $teamIds))
->getQuery()
->execute();
// used in UserTeamProjects widget
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL team.{id}', 'projects')
/** @var array<Team> $teams */
$teams = $qb->select('PARTIAL team.{id}', 'projects')
->from(Team::class, 'team')
->leftJoin('team.projects', 'projects')
->andWhere($qb->expr()->in('team.id', $ids))
->andWhere($qb->expr()->in('team.id', $teamIds))
->getQuery()
->execute();
$projectIds = [];
foreach ($results as $team) {
foreach ($team->getProjects() as $project) {
$projectIds[] = $project->getId();
}
}
if ($this->loadCustomer) {
// used in UserTeamProjects widget
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL project.{id}', 'customer')
->from(Project::class, 'project')
->leftJoin('project.customer', 'customer')
->andWhere($qb->expr()->in('project.id', $projectIds))
->getQuery()
->execute();
}
}
}

View File

@@ -15,46 +15,40 @@ use App\Entity\Project;
use App\Entity\Timesheet;
use Doctrine\ORM\EntityManagerInterface;
/**
* @internal
* @implements LoaderInterface<Timesheet>
*/
final class TimesheetLoader implements LoaderInterface
{
public function __construct(private EntityManagerInterface $entityManager, private bool $fullyHydrated = false, private bool $basicHydrated = true)
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly bool $fullyHydrated = false
)
{
}
/**
* @param array<int|Timesheet> $results
* @param array<Timesheet> $results
*/
public function loadResults(array $results): void
{
if (empty($results)) {
if (\count($results) === 0) {
return;
}
$ids = array_map(function ($timesheet) {
if ($timesheet instanceof Timesheet) {
// make sure that this potential doctrine proxy is initialized and filled with all data
$timesheet->getType();
$ids = array_filter(array_unique(array_map(function (Timesheet $timesheet) {
// make sure that this potential doctrine proxy is initialized and filled with all data
$timesheet->getType();
return $timesheet->getId();
}
return $timesheet;
}, $results);
return $timesheet->getId();
}, $results)), function ($value) { return $value !== null; });
$em = $this->entityManager;
$qb = $em->createQueryBuilder();
/** @var array<Timesheet> $timesheets */
$timesheets = $qb->select('PARTIAL t.{id}', 'project')
->from(Timesheet::class, 't')
->leftJoin('t.project', 'project')
->andWhere($qb->expr()->in('t.id', $ids))
->getQuery()
->execute();
$projectIds = array_map(function ($timesheet) {
return $timesheet->getProject()->getId();
}, $timesheets);
$projectIds = array_filter(array_unique(array_map(function (Timesheet $timesheet) {
return $timesheet->getProject()?->getId();
}, $results)), function ($value) { return $value !== null; });
if ($this->fullyHydrated) {
$qb = $em->createQueryBuilder();
@@ -76,9 +70,9 @@ final class TimesheetLoader implements LoaderInterface
->execute();
if ($this->fullyHydrated) {
$customerIds = array_map(function ($project) {
$customerIds = array_filter(array_unique(array_map(function (Project $project) {
return $project->getCustomer()->getId();
}, $projects);
}, $projects)), function ($value) { return $value !== null; });
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL c.{id}', 'meta')
@@ -89,18 +83,10 @@ final class TimesheetLoader implements LoaderInterface
->execute();
}
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL t.{id}', 'activity')
->from(Timesheet::class, 't')
->leftJoin('t.activity', 'activity')
->andWhere($qb->expr()->in('t.id', $ids))
->getQuery()
->execute();
if ($this->fullyHydrated) {
$activityIds = array_filter(array_map(function (Timesheet $timesheet) {
return $timesheet->getActivity()?->getId();
}, $timesheets), function ($id): bool {
}, $results), function ($id): bool {
return $id !== null;
});
@@ -113,30 +99,12 @@ final class TimesheetLoader implements LoaderInterface
->execute();
}
if ($this->basicHydrated) {
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL t.{id}', 'user')
->from(Timesheet::class, 't')
->leftJoin('t.user', 'user')
->andWhere($qb->expr()->in('t.id', $ids))
->getQuery()
->execute();
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL t.{id}', 'tags')
->from(Timesheet::class, 't')
->leftJoin('t.tags', 'tags')
->andWhere($qb->expr()->in('t.id', $ids))
->getQuery()
->execute();
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL t.{id}', 'meta')
->from(Timesheet::class, 't')
->leftJoin('t.meta', 'meta')
->andWhere($qb->expr()->in('t.id', $ids))
->getQuery()
->execute();
}
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL t.{id}', 'tags')
->from(Timesheet::class, 't')
->leftJoin('t.tags', 'tags')
->andWhere($qb->expr()->in('t.id', $ids))
->getQuery()
->execute();
}
}

View File

@@ -13,49 +13,54 @@ use App\Entity\Team;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
/**
* @internal
* @implements LoaderInterface<User>
*/
final class UserLoader implements LoaderInterface
{
public function __construct(private EntityManagerInterface $entityManager, private bool $fullyHydrated = false)
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly bool $fullyHydrated = false
)
{
}
/**
* @param array<int|User> $results
* @param array<User> $results
*/
public function loadResults(array $results): void
{
if (empty($results)) {
if (\count($results) === 0) {
return;
}
$ids = array_map(function ($user) {
if ($user instanceof User) {
// make sure that this potential doctrine proxy is initialized and filled with all data
$user->getDisplayName();
$userIds = array_filter(array_unique(array_map(function (User $user) {
// make sure that this potential doctrine proxy is initialized and filled with all data
$user->getDisplayName();
return $user->getId();
}
return $user;
}, $results);
return $user->getId();
}, $results)), function ($value) { return $value !== null; });
$em = $this->entityManager;
// this is currently needed, as it does not work via the Doctrine eager fetch method
// on user listing pages, if users are already in the unit of work from another load
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL user.{id}', 'preferences')
->from(User::class, 'user')
->leftJoin('user.preferences', 'preferences')
->andWhere($qb->expr()->in('user.id', $userIds))
->getQuery()
->execute();
$qb = $em->createQueryBuilder();
/** @var User[] $users */
$users = $qb->select('PARTIAL user.{id}', 'memberships', 'team')
->from(User::class, 'user')
->leftJoin('user.memberships', 'memberships')
->leftJoin('memberships.team', 'team')
->andWhere($qb->expr()->in('user.id', $ids))
->getQuery()
->execute();
$qb = $em->createQueryBuilder();
$qb->select('PARTIAL user.{id}', 'preferences')
->from(User::class, 'user')
->leftJoin('user.preferences', 'preferences')
->andWhere($qb->expr()->in('user.id', $ids))
->andWhere($qb->expr()->in('user.id', $userIds))
->getQuery()
->execute();