Skip to content

Commit

Permalink
Build entity with metadata identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrerolland committed Feb 5, 2018
1 parent 17167d5 commit ad9557c
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions ParamConverter/EntityParamConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit ad9557c

Please sign in to comment.