Skip to content
This repository has been archived by the owner on Feb 24, 2023. It is now read-only.

Commit

Permalink
fixed logic from previous PR
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Mar 24, 2015
1 parent bdf6df7 commit 1c3ed35
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 27 deletions.
38 changes: 16 additions & 22 deletions Request/ParamConverter/DoctrineParamConverter.php
Expand Up @@ -90,12 +90,10 @@ protected function find($class, Request $request, $options, $name)
}

try {
$result = $this->getManager($options['entity_manager'], $class)->getRepository($class)->$method($id);
return $this->getManager($options['entity_manager'], $class)->getRepository($class)->$method($id);
} catch (NoResultException $e) {
$result = null;
return;
}

return $result;
}

protected function getIdentifier(Request $request, $options, $name)
Expand Down Expand Up @@ -177,35 +175,31 @@ protected function findOneBy($class, Request $request, $options)

try {
if ($mapMethodSignature) {
$result = $this->findDataByMapMethodSignature($em, $class, $repositoryMethod, $criteria);
} else {
$result = $em->getRepository($class)->$repositoryMethod($criteria);
return $this->findDataByMapMethodSignature($em, $class, $repositoryMethod, $criteria);
}

return $em->getRepository($class)->$repositoryMethod($criteria);
} catch (NoResultException $e) {
$result = null;
return;
}

return $result;
}

private function findDataByMapMethodSignature($em, $class, $repositoryMethod, $criteria)
{
$arguments = array();
$repository = $em->getRepository($class);
$repositoryClass = get_class($repository);

$reflectionMethod = new \ReflectionMethod($repositoryClass, $repositoryMethod);
$parameters = array();
for ($i = 0; $i < count($criteria); $i++) {
$parameterName = new \ReflectionParameter(array($repositoryClass, $repositoryMethod), $i);
$parameterName = $parameterName->name;
if (!in_array($parameterName, array_keys($criteria))) {
throw new \InvalidArgumentException(sprintf('Parameter %s in %s::%s should be "%s"!', $i + 1, $repositoryClass, $repositoryMethod, $parameterName));
$ref = new \ReflectionMethod($repository, $repositoryMethod);
foreach ($ref->getParameters() as $parameter) {
if (array_key_exists($parameter->name, $criteria)) {
$arguments[] = $criteria[$parameter->name];
} elseif ($parameter->isDefaultValueAvailable()) {
$arguments[] = $parameter->getDefaultValue();
} else {
throw new \InvalidArgumentException(sprintf('Repository method "%s::%s" requires that you provide a value for the "$%s" argument.', get_class($repository), $repositoryMethod, $parameter->name));
}

$parameters[$parameterName] = $criteria[$parameterName];
}

return $reflectionMethod->invokeArgs($repository, $parameters);
return $ref->invokeArgs($repository, $arguments);
}

/**
Expand Down
7 changes: 3 additions & 4 deletions Resources/doc/annotations/converters.rst
Expand Up @@ -183,7 +183,7 @@ methods.

There are cases where you want to you use your own repository method and you
want to map the criteria to the method signature. This is possible when you set
the ``map_method_signature`` option to true. The default is false.
the ``map_method_signature`` option to true. The default is false::

/**
* @Route("/user/{first_name}/{last_name}")
Expand All @@ -207,9 +207,8 @@ the ``map_method_signature`` option to true. The default is false.

.. tip::

With de default implementation, the ``firstName`` and ``lastName``
parameters have to be Doctrine fields. When using ``map_method_signature``,
they don't have to be known by Doctrine.
When ``map_method_signature`` is ``true``, the ``firstName`` and
``lastName`` parameters do not have to be Doctrine fields.

DateTime Converter
~~~~~~~~~~~~~~~~~~
Expand Down
Expand Up @@ -376,7 +376,7 @@ public function testApplyWithRepositoryMethodAndMapMethodSignature()

/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Parameter 2 in Sensio\Bundle\FrameworkExtraBundle\Tests\Request\ParamConverter\TestUserRepository::findByFullName should be "lastName"!
* @expectedExceptionMessage Repository method "Sensio\Bundle\FrameworkExtraBundle\Tests\Request\ParamConverter\TestUserRepository::findByFullName" requires that you provide a value for the "$lastName" argument.
*/
public function testApplyWithRepositoryMethodAndMapMethodSignatureException()
{
Expand Down

0 comments on commit 1c3ed35

Please sign in to comment.