Skip to content
This repository

Enable PropelParamConverter to find on ->getName() #154

Closed
wants to merge 1 commit into from

4 participants

cedric lombardot Don't Add Me To Your Organization a.k.a The Travis Bot William Durand Jérémie Augustin
cedric lombardot

This make easiest Param conversion for esi

<?php

/**
  * @ParamConverter("book", class="MyCoreBundle:Model:Core:Book")
  */
    public function renderAction(Book $book)
    {
        return $this->render('....', array(
            'book' => $book,
        ));
    }
Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged 73aa53c into 2387821).

William Durand
Owner

I think I don't understand the purpose of this feature... can you elaborate a bit more?

cedric lombardot

For my controller the esi internal url generated look like :

frontend_dev.php/_internal/secure/my.core.block.list%3ArenderAction/ block%3D1 .html

And my controller look like :

    /**
     * Render a block
     *
     * @ParamConverter("block", class="XXXXCoreBundle:Model:Core:Block")
     *
     * @param Block $block
     */
    public function renderAction(Block $block)
    {
        ///
    }

Or actually the paramConverter try to find by the object $block using the id parameter or a findOnByBlock but not a findById($_GET['block']) that what enabled the PR

William Durand
Owner

I still don't get the point, but let's wait for @jaugustin's work on the converter.

Jérémie Augustin

To do that, your parameter should be named "id" (in your request)

cedric lombardot

Yes but my problem is thats not my url but the symfony _internal for ESI

Jérémie Augustin

I don't know how symfony handle internal route for ESI, does doctrineParamConverter handle it the right way ?

Jérémie Augustin

after our talk I think the right way to use PropelParamConverter with ESI is the same as with normal route :

set parameter : id => $book->getId()
or : slug => $book->getSlug() if you have a filterBySlug in the query class

what you want could be understanded as a mapping option:

<?php

/**
 * @ParamConverter("book", class="Model\Book", options={"mapping" : { "routeParam1" : "id"} })
 * @ParamConverter("author", class="Model\Author", options={"mapping" : { "routeParam2" : "slug"} })
 */
public function myAction(Book $book, Author $author)
{ }
William Durand
Owner

Ok, I'm closing it.

William Durand willdurand closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

May 10, 2012
cedric lombardot cedriclombardot Enable PropelParamConverter to find on ->getName()
This make easiest Param conversion for esi
73aa53c
This page is out of date. Refresh to see the latest.
12 Request/ParamConverter/PropelParamConverter.php
@@ -27,7 +27,7 @@ public function apply(Request $request, ConfigurationInterface $configuration)
27 27 $exclude = isset($options['exclude'])? $options['exclude'] : array();
28 28
29 29 // find by Pk
30   - if (in_array('id', $exclude) || false === $object = $this->findPk($classQuery, $request)) {
  30 + if (in_array('id', $exclude) || false === $object = $this->findPk($classQuery, $request, $configuration->getName())) {
31 31 // find by criteria
32 32 if (false === $object = $this->findOneBy($classQuery, $request, $exclude)) {
33 33 throw new \LogicException('Unable to guess how to get a Propel object from the request information.');
@@ -41,13 +41,17 @@ public function apply(Request $request, ConfigurationInterface $configuration)
41 41 $request->attributes->set($configuration->getName(), $object);
42 42 }
43 43
44   - protected function findPk($classQuery, Request $request)
  44 + protected function findPk($classQuery, Request $request, $pk)
45 45 {
46   - if (!$request->attributes->has('id')) {
  46 + if (!$request->attributes->has($pk) || '' == $request->attributes->get($pk)) {
  47 + $pk = 'id';
  48 + }
  49 +
  50 + if (!$request->attributes->has($pk)) {
47 51 return false;
48 52 }
49 53
50   - return $classQuery::create()->findPk($request->attributes->get('id'));
  54 + return $classQuery::create()->findPk($request->attributes->get($pk));
51 55 }
52 56
53 57 protected function findOneBy($classQuery, Request $request, $exclude)
10 Tests/Request/ParamConverter/PropelParamConverterTest.php
@@ -55,6 +55,16 @@ public function testParamConverterFindPkNotFound()
55 55 $paramConverter->apply($request, $configuration);
56 56 }
57 57
  58 + public function testFindPkOnBook()
  59 + {
  60 + $paramConverter = new PropelParamConverter();
  61 + $request = new Request(array(), array(), array('book' => 1));
  62 + $configuration = new ParamConverter(array('class' => 'Propel\PropelBundle\Tests\Fixtures\Model\Book', 'name' => 'book'));
  63 + $paramConverter->apply($request, $configuration);
  64 + $this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\Model\Book',$request->attributes->get('book'),
  65 + 'param "book" should be an instance of "Propel\PropelBundle\Tests\Fixtures\Model\Book"');
  66 + }
  67 +
58 68 public function testParamConverterFindSlug()
59 69 {
60 70 $paramConverter = new PropelParamConverter();

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.