beni0888
beni0888

Reputation: 1244

Passing parameters to Symfony @ParamConverter

I'm using symfony @ParamConverter in my controller to get an entity by its slug, but instead of use the "findBySlug()" method I'm using a more specific one "findWithSomeFeaturesBySlug($slug)", and I have a problem because in the findWithSomeFeaturesBySlug($slug) method I receive the $slug parameter as an associative array with an 'slug' key instead of the slug's value.

My controller's code looks like the following:

/**
* @Route("/some-route/{slug}")
* @ParamConverter("object", class="AcmeBundle:Object", options={"repository_method" = "findWithSomeFeaturesBySlug"})
*/
public function acmeDemoAction(Object $object)
{
    // Controller code here
}

I hope that someone can help me.

Thanks.

UPDATE:

I'm sorry, I think that I didn't explain my problem correctly. The problem is that I get an associative array in the "findWithSomeFeaturesBySlug($slug)" function and I need to get the $slug value directly.

// ObjectRepository
public function findWithSomeFeatures($slug) 
{
    // here I get an associative array in the slug parameter:
    // $slug = array('slug' => 'some_value')
    // And I need $slug = 'some_value'
}

Upvotes: 2

Views: 6206

Answers (2)

LFI
LFI

Reputation: 654

I had the same issue, fixed by adding the id option in the ParamConverter

/**
* @Route("/some-route/{slug}")
* @ParamConverter("object", class="AcmeBundle:Object", options={"id" = "slug", "repository_method" = "findWithSomeFeaturesBySlug"})
*/
public function acmeDemoAction(Object $object)
{
    // Controller code here
}

Upvotes: 8

Maks3w
Maks3w

Reputation: 6439

Create a new method in your repository for proxy the call

findSlugXXXX($slug) {
    $this->findWithSomeFeaturesBySlug(array('slug' => $slug));
}

Upvotes: 0

Related Questions