fix param converter to handle correctly isOptional parameter

This commit is contained in:
Jeremie Augustin 2012-05-07 18:43:27 +02:00
parent 2e7bb13431
commit a3871ccc74
2 changed files with 19 additions and 2 deletions

View file

@ -29,8 +29,13 @@ class PropelParamConverter implements ParamConverterInterface
// find by Pk
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 (false === ($object = $this->findOneBy($classQuery, $request, $exclude))) {
if ($configuration->isOptional()) {
//we find nothing but the obejct is optional
$object = null;
} else {
throw new \LogicException('Unable to guess how to get a Propel object from the request information.');
}
}
}

View file

@ -128,4 +128,16 @@ 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', 'optional' => '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');
}
}