Skip to content

Commit

Permalink
Made fetching by other fields than identifiers possible
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrerolland committed Dec 14, 2020
1 parent 163d3a3 commit 85b6f55
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 19 deletions.
67 changes: 48 additions & 19 deletions ParamConverter/EntityParamConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,12 @@ public function __construct(EntityBuilder $builder, EntityManager $entityManager
function apply(Request $request, ParamConverter $configuration)
{
$class = $configuration->getClass();
$identifiers = $this->entityManager->getClassMetadata($class)->getIdentifierFieldNames();
$options = $configuration->getOptions();

$search = [];
foreach ($identifiers as $identifier) {
try {
$value = $this->requestFinder->find($identifier, $request);
if ($value !== null) {
$search[$identifier] = $value;
}
} catch (FieldNotFoundInRequestException $e) {
// continue
}
}

if (count($search) === count($identifiers)) {
$entity = $this->entityManager->getRepository($class)->findOneBy($search);
if (!$entity) {
$entity = new $class();
}
if (isset($options['properties'])) {
$entity = $this->retrieveEntity($class, $request, $options['properties']);
} else {
$entity = new $class();
$entity = $this->retrieveFromIdentifiers($class, $request);
}

$this->builder->buildEntity($entity, $request);
Expand All @@ -91,4 +76,48 @@ function supports(ParamConverter $configuration)
return false;
}
}

/**
* @param string $class
* @param Request $request
*
* @return object
*/
private function retrieveFromIdentifiers($class, Request $request)
{
return $this->retrieveEntity($class, $request, $this->entityManager->getClassMetadata($class)->getIdentifierFieldNames());
}

/**
* @param string $class
* @param Request $request
* @param array $identifiers
*
* @return object
*/
private function retrieveEntity($class, Request $request, array $identifiers)
{
$search = [];
foreach ($identifiers as $identifier) {
try {
$value = $this->requestFinder->find($identifier, $request);
if ($value !== null) {
$search[$identifier] = $value;
}
} catch (FieldNotFoundInRequestException $e) {
// continue
}
}

if (count($search) === count($identifiers)) {
$entity = $this->entityManager->getRepository($class)->findOneBy($search);
if (!$entity) {
$entity = new $class();
}
} else {
$entity = new $class();
}

return $entity;
}
}
35 changes: 35 additions & 0 deletions Resources/doc/doctrine_entity.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,38 @@ You will end up with these tables:
| 1 | 3 |

Exactly what had been defined inside your JSON object.

### Retrieve an entity using other fields than the identifiers

Use the `properties` option:

```php
<?php

namespace App\Controller;

use App\Entity\Article;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;

class ArticleController extends Controller
{
/**
* @param Article $article
*
* @return Response
*
* @ParamConverter(
* name="article",
* class="App\Entity\Article",
* converter="rollandrock_entity_converter",
* options={properties:{"slug"}}
* )
*/
public function postAction(Article $article)
{
$this->get('app.manager.article')->save($article);

return $this->handleResponse($article, Response::HTTP_CREATED);
}
}
```

0 comments on commit 85b6f55

Please sign in to comment.