Permalink
Browse files

Modified the PropelParamConverter to allow CamelCased parameters

Currently, parameters like "author_slug" are translated by the param
converter to a "->filterByAuthor_slug()" call. I changed this behavior
and they are now translated to "->filterByAuthorSlug()".
  • Loading branch information...
1 parent 2387821 commit b54cf4ab3f4b0e8c5f399ef099d8d454b491cc7c @K-Phoen K-Phoen committed with willdurand May 19, 2012
@@ -2,6 +2,7 @@
namespace Propel\PropelBundle\Request\ParamConverter;
+use Propel\PropelBundle\Util\PropelInflector;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@@ -62,7 +63,7 @@ protected function findOneBy($classQuery, Request $request, $exclude)
foreach ($request->attributes->all() as $key => $value) {
if (!in_array($key, $exclude)) {
try {
- $query->{'filterBy' . ucfirst($key)}($value);
+ $query->{'filterBy' . PropelInflector::camelize($key)}($value);
$hasCriteria = true;
} catch (\PropelException $e) { }
}
@@ -18,6 +18,7 @@
class BookQuery extends BaseBookQuery {
private $bySlug = false;
+ private $byAuthorSlug = false;
/**
* fake for test
@@ -37,6 +38,18 @@ public function findPk($key, $con = null)
/**
* fake for test
*/
+ public function filterByAuthorSlug($slug = null, $comparison = null)
+ {
+ if ('my-author' === $slug) {
+ $this->byAuthorSlug = true;
+ }
+
+ return $this;
+ }
+
+ /**
+ * fake for test
+ */
public function filterBySlug($slug = null, $comparison = null)
{
if ('my-book' == $slug) {
@@ -66,6 +79,13 @@ public function findOne($con = null)
$book->setSlug('my-book');
return $book;
+ } else if (true === $this->byAuthorSlug) {
+ $book = new Book();
+ $book->setId(2);
+ $book->setName('My Kewl Book');
+ $book->setSlug('my-kewl-book');
+
+ return $book;
}
return null;
@@ -65,6 +65,17 @@ public function testParamConverterFindSlug()
'param "book" should be an instance of "Propel\PropelBundle\Tests\Fixtures\Model\Book"');
}
+ public function testParamConverterFindCamelCasedSlug()
+ {
+ $paramConverter = new PropelParamConverter();
+ $request = new Request(array(), array(), array('author_slug' => 'my-author', 'slug' => 'my-kewl-book', 'book' => null));
+ $configuration = new ParamConverter(array('class' => 'Propel\PropelBundle\Tests\Fixtures\Model\Book', 'name' => 'book'));
+
+ $paramConverter->apply($request, $configuration);
+ $this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\Model\Book',$request->attributes->get('book'),
+ 'param "book" should be an instance of "Propel\PropelBundle\Tests\Fixtures\Model\Book"');
+ }
+
/**
* @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/

0 comments on commit b54cf4a

Please sign in to comment.