From cf2d66976683b457a6fbba2b12a62552700480e0 Mon Sep 17 00:00:00 2001 From: Peter Bowyer Date: Wed, 20 Feb 2019 12:10:02 +0000 Subject: [PATCH] Allow filtering on natural keys The bundle assumed surrogate keys, and returned `Type::INTEGER` for all. This change uses class metadata to get the correct type. --- Event/Subscriber/DoctrineORMSubscriber.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Event/Subscriber/DoctrineORMSubscriber.php b/Event/Subscriber/DoctrineORMSubscriber.php index b8cc6e8..206ea12 100644 --- a/Event/Subscriber/DoctrineORMSubscriber.php +++ b/Event/Subscriber/DoctrineORMSubscriber.php @@ -108,7 +108,7 @@ public function filterEntity(GetFilterConditionEvent $event) $expr->eq($filterField, ':'.$paramName), array($paramName => array( $this->getEntityIdentifier($values['value'], $queryBuilder->getEntityManager()), - Type::INTEGER + $this->getEntityIdentifierType($values['value'], $queryBuilder->getEntityManager()), )) ); } @@ -137,4 +137,23 @@ protected function getEntityIdentifier($value, EntityManagerInterface $em) return array_shift($identifierValues); } + + /** + * @param object $value + * @return string + * @throws \RuntimeException + */ + protected function getEntityIdentifierType($value, EntityManagerInterface $em) + { + $class = get_class($value); + $metadata = $em->getClassMetadata($class); + + $identifierType = $metadata->getIdentifierFieldNames($value); + + if (empty($identifierType)) { + throw new \RuntimeException(sprintf('Can\'t get identifier value for class "%s".', $class)); + } + + return $metadata->getTypeOfField(array_shift($identifierType)); + } }