fix: reduce number of database queries caused by voter

This commit is contained in:
Björn Fromme
2025-04-11 09:51:32 +02:00
parent 947b183c50
commit 24ffee7c0b
6 changed files with 38 additions and 15 deletions
+1 -1
View File
@@ -91,4 +91,4 @@ class IndexController extends AbstractController
return $dispositions;
}
}
}
+13
View File
@@ -464,6 +464,19 @@ class Assignment implements BlameableEntityInterface, TimestampableEntityInterfa
return $this;
}
public function getDispositionByTeamer(Teamer $teamer): ?Disposition
{
$dispositions = $this->dispositions->filter(function (Disposition $disposition) use ($teamer) {
return $disposition->getTeamer() === $teamer;
});
if (0 === count($dispositions)) {
return null;
}
return $dispositions->first();
}
public function getValidDispositions(): Collection
{
return $this->dispositions->filter(function (Disposition $disposition) {
+14
View File
@@ -87,6 +87,13 @@ class AssignmentRepository extends ServiceEntityRepository
->setParameter('now', new \DateTimeImmutable())
;
if (0 === $teamer->getTrainingAttendances()->count() && 0 === $teamer->getLicenses()->count()) {
$qb
->andWhere($qb->expr()->eq($qb->expr()->count('job_profile.requiredLicenses'), 0))
->andWhere($qb->expr()->eq($qb->expr()->count('job_profile.requiredTrainings'), 0))
;
}
$this->applyFilterSettings($filterDto, $qb);
return $qb->getQuery();
@@ -280,6 +287,13 @@ class AssignmentRepository extends ServiceEntityRepository
->setParameter('dateFrom', new \DateTimeImmutable())
;
if (0 === $teamer->getTrainingAttendances()->count() && 0 === $teamer->getLicenses()->count()) {
$qb
->andWhere($qb->expr()->eq($qb->expr()->count('job_profile.requiredLicenses'), 0))
->andWhere($qb->expr()->eq($qb->expr()->count('job_profile.requiredTrainings'), 0))
;
}
return $qb
->orderBy('assignment.createdAt', 'DESC')
->setMaxResults($limit)
+2 -1
View File
@@ -97,8 +97,9 @@ class DispositionRepository extends ServiceEntityRepository
$qb = $this->createQueryBuilder('disposition');
return $qb
->select('disposition', 'teamer', 'feedback')
->select('disposition', 'teamer', 'user', 'feedback')
->innerJoin('disposition.teamer', 'teamer')
->leftJoin('teamer.user', 'user')
->leftJoin('teamer.feedback', 'feedback')
->where($qb->expr()->eq('disposition.assignment', ':assignment'))
->setParameter('assignment', $assignment)
+2 -8
View File
@@ -74,13 +74,7 @@ class AssignmentVoter extends Voter
// Only allow applications on empty slots
$availableSlots = (int) $assignment->getAvailableDispositions();
if (0 < $availableSlots) {
$dispositions = $this
->entityManager
->getRepository(Disposition::class)
->findCurrentDispositionsByAssignment($assignment)
;
return count($dispositions) < $availableSlots;
return $assignment->getDispositions()->count() < $availableSlots;
}
// Only teamers without existing applications may apply to the assignment
@@ -98,4 +92,4 @@ class AssignmentVoter extends Voter
return false;
}
}
}
+6 -5
View File
@@ -57,8 +57,9 @@
{% endif %}
</div>
<div>
{% if assignment.applications|length %}
{% set application = assignment.applicationByTeamer(teamer) %}
{% set application = assignment.applications.first %}
{% set disposition = assignment.dispositions.first%}
{% if application %}
{% if application.status == constant('App\\Entity\\Application::STATUS_REJECTED') %}
<a href="{{ path('app_teamer_assignment_detail', { 'uuid': assignment.uuid, 'r': return_url() }) }}"
class="btn btn--small flex items-center space-x-2 bg-gray-100 text-gray-800 hover:bg-gray-50">
@@ -72,8 +73,8 @@
<span>in Bearbeitung</span>
</a>
{% endif %}
{% elseif assignment.dispositions|length %}
<a href="{{ path('app_teamer_disposition_detail', { 'uuid': assignment.dispositions[0].uuid }) }}"
{% elseif disposition %}
<a href="{{ path('app_teamer_disposition_detail', { 'uuid': disposition.uuid }) }}"
class="btn btn--small flex items-center space-x-2 bg-green-100 text-green-700 hover:bg-green-50">
{{ icon('check', 'w-4 h-4 shrink-0') }}
<span>eingeteilt</span>
@@ -98,4 +99,4 @@
{% endfor %}
</div>
{{ knp_pagination_render(pagination, 'paginator/sliding_boxes.html.twig') }}
{% endblock %}
{% endblock %}