Merge pull request #150 from jaugustin/fix-param-converter-optional
fix param converter to handle correctly isOptional parameter
This commit is contained in:
commit
c02e7ceb6c
|
@ -30,7 +30,12 @@ class PropelParamConverter implements ParamConverterInterface
|
|||
if (in_array('id', $exclude) || false === $object = $this->findPk($classQuery, $request)) {
|
||||
// find by criteria
|
||||
if (false === $object = $this->findOneBy($classQuery, $request, $exclude)) {
|
||||
throw new \LogicException('Unable to guess how to get a Propel object from the request information.');
|
||||
if ($configuration->isOptional()) {
|
||||
//we find nothing but the object is optional
|
||||
$object = null;
|
||||
} else {
|
||||
throw new \LogicException('Unable to guess how to get a Propel object from the request information.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -128,4 +128,17 @@ class PropelParamConverterTest extends TestCase
|
|||
$configuration = new ParamConverter(array('class' => 'Propel\PropelBundle\Tests\Fixtures\Model\Book', 'name' => 'book'));
|
||||
$paramConverter->apply($request, $configuration);
|
||||
}
|
||||
|
||||
|
||||
public function testParamConverterFindWithOptionalParam()
|
||||
{
|
||||
$paramConverter = new PropelParamConverter();
|
||||
$request = new Request(array(), array(), array('book' => null));
|
||||
$configuration = new ParamConverter(array('class' => 'Propel\PropelBundle\Tests\Fixtures\Model\Book', 'name' => 'book'));
|
||||
$configuration->setIsOptional(true);
|
||||
$paramConverter->apply($request, $configuration);
|
||||
|
||||
$this->assertNull($request->attributes->get('book'),
|
||||
'param "book" should be null if book is not found and the parameter is optional');
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue