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:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user