From 6eed6900e8734b50f45ae1370d7c235031c61ed9 Mon Sep 17 00:00:00 2001 From: Ali OGUZ Date: Tue, 16 Apr 2024 10:11:38 +0200 Subject: [PATCH] Fix field name generation and accept string identifier for filter entity --- Event/Subscriber/DoctrineORMSubscriber.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Event/Subscriber/DoctrineORMSubscriber.php b/Event/Subscriber/DoctrineORMSubscriber.php index f874001..2c9e2e8 100644 --- a/Event/Subscriber/DoctrineORMSubscriber.php +++ b/Event/Subscriber/DoctrineORMSubscriber.php @@ -12,6 +12,7 @@ namespace Spiriit\Bundle\FormFilterBundle\Event\Subscriber; use Doctrine\Common\Collections\Collection; +use Doctrine\DBAL\ArrayParameterType; use Doctrine\DBAL\Connection; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\EntityManagerInterface; @@ -86,7 +87,7 @@ public function filterEntity(GetFilterConditionEvent $event) if ($dqlFrom = $event->getQueryBuilder()->getDQLPart('from')) { $rootPart = reset($dqlFrom); - $fieldName = ltrim($event->getField(), $rootPart->getAlias() . '.'); + $fieldName = \str_replace(\sprintf('%s.', $rootPart->getAlias()), null, $event->getField()); $metadata = $queryBuilder->getEntityManager()->getClassMetadata($rootPart->getFrom()); if (isset($metadata->associationMappings[$fieldName]) && (!$metadata->associationMappings[$fieldName]['isOwningSide'] || $metadata->associationMappings[$fieldName]['type'] === ClassMetadataInfo::MANY_TO_MANY)) { @@ -108,13 +109,15 @@ public function filterEntity(GetFilterConditionEvent $event) if (count($ids) > 0) { $event->setCondition( $expr->in($filterField, ':' . $paramName), - [$paramName => [$ids, Connection::PARAM_INT_ARRAY]] + [$paramName => [$ids, \is_int($ids[0]) ? ArrayParameterType::INTEGER : ArrayParameterType::STRING]] ); } } else { + $id = $this->getEntityIdentifier($values['value'], $queryBuilder->getEntityManager()); + $event->setCondition( $expr->eq($filterField, ':' . $paramName), - [$paramName => [$this->getEntityIdentifier($values['value'], $queryBuilder->getEntityManager()), Types::INTEGER]] + [$paramName => [$id, \is_int($id) ? Types::INTEGER : Types::STRING]] ); } }