Skip to content

Commit

Permalink
Fix field name generation and accept string identifier for filter ent…
Browse files Browse the repository at this point in the history
…ity (#21)

Co-authored-by: Ali OGUZ <[email protected]>
  • Loading branch information
alioguz and Ali OGUZ authored Jul 31, 2024
1 parent daf2ae9 commit 1966b3b
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions Event/Subscriber/DoctrineORMSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand All @@ -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]]
);
}
}
Expand Down

0 comments on commit 1966b3b

Please sign in to comment.