Permalink
Browse files

fix param converter to handle correctly isOptional parameter

  • Loading branch information...
1 parent 2e7bb13 commit a3871ccc74aa99838eb86ebab3b2704d2b163693 @jaugustin jaugustin committed May 7, 2012
@@ -29,8 +29,13 @@ public function apply(Request $request, ConfigurationInterface $configuration)
// 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.');
+ }
}
}
@@ -128,4 +128,16 @@ public function testParamConverterFindLogicError()
$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');
+ }
}

0 comments on commit a3871cc

Please sign in to comment.