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

Commit

Permalink
Allow filtering on natural keys
Browse files Browse the repository at this point in the history
The bundle assumed surrogate keys, and returned `Type::INTEGER` for all. This change uses class metadata to get the correct type.
  • Loading branch information
pbowyer authored Feb 20, 2019
1 parent 25f5b77 commit 2a0b01c
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion Event/Subscriber/DoctrineORMSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
))
);
}
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 2a0b01c

Please sign in to comment.