From ad9557c126adec450a712a19aadbd965ea43da6f Mon Sep 17 00:00:00 2001 From: Pierre Rolland Date: Mon, 5 Feb 2018 22:38:17 +0100 Subject: [PATCH] Build entity with metadata identifiers --- ParamConverter/EntityParamConverter.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/ParamConverter/EntityParamConverter.php b/ParamConverter/EntityParamConverter.php index f9545df..3d5aed1 100644 --- a/ParamConverter/EntityParamConverter.php +++ b/ParamConverter/EntityParamConverter.php @@ -43,11 +43,19 @@ public function __construct(EntityBuilder $builder, EntityManager $entityManager */ function apply(Request $request, ParamConverter $configuration) { - $id = $this->requestFinder->find('id', $request); $class = $configuration->getClass(); + $identifiers = $this->entityManager->getClassMetadata($class)->getIdentifierFieldNames(); - if ($id) { - $entity = $this->entityManager->getRepository($configuration->getClass())->find($id); + $search = []; + foreach ($identifiers as $identifier) { + $value = $this->requestFinder->find($identifier, $request); + if ($value !== null) { + $search[$identifier] = $value; + } + } + + if (count($search) === count($identifiers)) { + $entity = $this->entityManager->getRepository($configuration->getClass())->findBy($search); if (!$entity) { $entity = new $class(); }