Merge pull request #150 from jaugustin/fix-param-converter-optional

fix param converter to handle correctly isOptional parameter
This commit is contained in:
William Durand 2012-05-07 15:06:20 -07:00
commit c02e7ceb6c
2 changed files with 19 additions and 1 deletions

View file

@ -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.');
}
}
}

View file

@ -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');
}
}