3

In an entity, I've define a slug with the DoctrineExtension, I use it in my href. But... I've a problem with the Router and/or ParamConverter.

In my controller:

/**
 * @Route("/{slug}", name="strain_view")
 * @ParamConverter("strain", class="AppBundle:Strain", options={
 *     "repository_method" = "findOneWithAll",
 * })
 * @Security("is_granted('STRAIN_VIEW', strain)")
 */
public function viewAction(Strain $strain)
{
    return $this->render('strain/view.html.twig', [
        'strain' => $strain,
    ]);
}

And I've this error:

SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

In the debug bar, I see, the problem is the array in array:

Parameters: [ 0 => [ slug => t1p-0004-e-coli-pgem-t-promupf3-leu2-termupf3 ] ]

When I replace in @Route: {slug} by {id} and I manually type the url, I've

Parameters: [0 => t1p-0004-e-coli-pgem-t-promupf3-leu2-termupf3]

And... it's work, but in my Repository I've:

public function findOneWithAll($slug)
{
    $query = $this->createQueryBuilder('strain')
            ->where('strain.slug = :slug')
            ->setParameter('slug', $slug)
            ->getQuery();

I don't understand why.

mpiot
  • 1,482
  • 2
  • 15
  • 36

2 Answers2

2

Simply :

/**
  * @ParamConverter("strain", options={"mapping": {"slug": "slug"}})
  */
1

You should try this

/**
 * @Route("/{slug}", name="strain_view")
 * @Entity("strain", expr="repository.findOneWithAll(slug)")
 * @Security("is_granted('STRAIN_VIEW', strain)")
 */
public function viewAction(Strain $strain)
{
    return $this->render('strain/view.html.twig', [
        'strain' => $strain,
    ]);
}
OlivierC
  • 682
  • 4
  • 11
  • Thanks, I've seen it in the doc but... I don't now which class I need to import... When I type use, I've try all thing that finish by Entity, but I've always a `[Semantical Error] The annotation "@Entity" in method was never imported. Did you maybe forget to add a "use" statement for this annotation?` – mpiot Feb 23 '17 at 18:11
  • Finally, I think when the param is not an id, he create an array of params. hen I rename it $param in the repository and I do a setParameter('slug', $param['slug']. I think there is no other way... – mpiot Feb 23 '17 at 19:11
  • This is actually what happens. If you an id the default method used by the paramConverter will be find otherwise it will be findOneBy so an array of params is given. I think the annotation to import is Doctrine\ORM\Mapping\Entity – OlivierC Feb 24 '17 at 08:41
  • Yes I think, when it's cal id the param converter just send it, when the name is not id, it do an array of params.It's the Entity annotation to declare an Entity. Annotation @Entity is not allowed to be declared on method. You may only use this annotation on these code elements: CLASS. – mpiot Feb 24 '17 at 09:01
  • It appears to be Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity which is only available for version 4+ of FrameworkExtraBundle – OlivierC Feb 24 '17 at 10:24
  • `Exception thrown when handling an exception (Symfony\Component\Config\Exception\LoaderLoadException: [Semantical Error] Annotation @Entity is not allowed to be declared on method App\Controller\NewsController::viewAction(). You may only use this annotation on these code elements: CLASS in /../../src/Controller/ (which is being imported from "config/routes/annotations.yaml"). Make sure annotations are installed and enabled.)` – HelpNeeder Jan 15 '19 at 19:38