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

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