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

Allow filtering on natural keys #303

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()),
Types::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));
}
}