SocialActionFilter: use a subquery "exists" instead of a where clause (many-to-many association)

This commit is contained in:
Julien Fastré 2023-09-13 12:26:54 +02:00
parent 981be7b363
commit 217232fe4f
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
2 changed files with 18 additions and 28 deletions

View File

@ -0,0 +1,5 @@
kind: Fixed
body: Fix filter "accompanying course by social action" to avoid duplication in list
time: 2023-09-13T12:26:16.38720953+02:00
custom:
Issue: "143"

View File

@ -12,28 +12,17 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters;
use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\SocialWork\SocialAction;
use Chill\PersonBundle\Export\Declarations;
use Chill\PersonBundle\Form\Type\PickSocialActionType;
use Chill\PersonBundle\Templating\Entity\SocialActionRender;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface;
use function in_array;
class SocialActionFilter implements FilterInterface
final readonly class SocialActionFilter implements FilterInterface
{
private SocialActionRender $actionRender;
private TranslatableStringHelper $translatableStringHelper;
public function __construct(
TranslatableStringHelper $translatableStringHelper,
SocialActionRender $actionRender
) {
$this->translatableStringHelper = $translatableStringHelper;
$this->actionRender = $actionRender;
}
public function __construct(private SocialActionRender $actionRender) {}
public function addRole(): ?string
{
@ -42,21 +31,17 @@ class SocialActionFilter implements FilterInterface
public function alterQuery(QueryBuilder $qb, $data)
{
if (!in_array('acpw', $qb->getAllAliases(), true)) {
$qb->join('acp.works', 'acpw');
}
if (!in_array('acpwsocialaction', $qb->getAllAliases(), true)) {
$qb->join('acpw.socialAction', 'acpwsocialaction');
}
$clause = $qb->expr()->in('acpwsocialaction.id', ':socialactions');
$qb->andWhere($clause)
->setParameter(
'socialactions',
SocialAction::getDescendantsWithThisForActions($data['accepted_socialactions'])->toArray()
$qb->andWhere(
$qb->expr()->exists(
sprintf(
"SELECT 1 FROM %s acp_by_social_action_filter WHERE acp_by_social_action_filter.socialAction "
. "IN (:acp_by_social_action_filter_actions) AND acp_by_social_action_filter.accompanyingPeriod = acp",
AccompanyingPeriod\AccompanyingPeriodWork::class
)
)
);
$qb->setParameter('acp_by_social_action_filter_actions', SocialAction::getDescendantsWithThisForActions($data['accepted_socialactions']));
}
public function applyOn(): string