Skip to content
This repository was archived by the owner on Aug 1, 2024. It is now read-only.

Commit 986c586

Browse files
committed
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.
1 parent ecea502 commit 986c586

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

Event/Subscriber/DoctrineORMSubscriber.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function filterEntity(GetFilterConditionEvent $event)
108108
$expr->eq($filterField, ':'.$paramName),
109109
array($paramName => array(
110110
$this->getEntityIdentifier($values['value'], $queryBuilder->getEntityManager()),
111-
Types::INTEGER
111+
$this->getEntityIdentifierType($values['value'], $queryBuilder->getEntityManager()),
112112
))
113113
);
114114
}
@@ -137,4 +137,23 @@ protected function getEntityIdentifier($value, EntityManagerInterface $em)
137137

138138
return array_shift($identifierValues);
139139
}
140+
141+
/**
142+
* @param object $value
143+
* @return string
144+
* @throws \RuntimeException
145+
*/
146+
protected function getEntityIdentifierType($value, EntityManagerInterface $em)
147+
{
148+
$class = get_class($value);
149+
$metadata = $em->getClassMetadata($class);
150+
151+
$identifierType = $metadata->getIdentifierFieldNames($value);
152+
153+
if (empty($identifierType)) {
154+
throw new \RuntimeException(sprintf('Can\'t get identifier value for class "%s".', $class));
155+
}
156+
157+
return $metadata->getTypeOfField(array_shift($identifierType));
158+
}
140159
}

0 commit comments

Comments
 (0)