0

When calling a method in my controller, I encounter an issue with the @ParamConverter annotation in Symfony. The specific error I am facing is: App\Entity\Recipe object not found by the @ParamConverter annotation.'

I have a findPublicRecipe() method in my RecipeRepository. This method is supposed to fetch public recipes based on an optional parameter, $nbRecipes. Here is the method in question.

    function findPublicRecipe(?int $nbRecipes): array
    {
        $queryBuilder = $this->createQueryBuilder('r')
            ->where('r.isPublic = 1')
            ->orderBy('r.createdAt', 'DESC');

        if ($nbRecipes !== 0 && $nbRecipes !== null) {
            $queryBuilder->setMaxResults($nbRecipes);
        }

        return $queryBuilder->getQuery()->getResult();
    }

I'm calling this findPublicRecipe() method in my controller without using the @ParamConverter annotation. Here's the code from my controller:

    #[Route('/recipe/public', name: 'recipe.index.public', methods: ['GET'])]
    public function indexPublic(
        RecipeRepository $repository,
        Request $request,
        PaginatorInterface $paginator,
    ): Response {
        $recipes = $repository->findPublicRecipe(null);
        $recipes = $paginator->paginate(
            $recipes,
            $request->query->getInt('page', 1),
            10
        );

        return $this->render('pages/recipe/indexPublic.html.twig', [
            'recipes' => $recipes
        ]);
    }

Despite this, I'm still receiving the error mentioned above. I've checked that the Recipe entity is correctly imported into my controller and that the namespace path is correct. Additionally, I've verified that the Recipe entity is appropriately defined and the annotations are appropriate. Could anyone please help me understand why this error happens and how to fix it?

1 Answer 1

0

One thing that needs to be verified is that you dont't define a dynamic route before this static route. I don't see the whole controller class, but I suspect that a dynamic route (e.g. #[Route('/recipe/{id}')] may be defined earlier, than the route in question. This dynamic route then gets matched earlier and could return the Not Found response you are dealing with.

Also, you might want to checkout the expression option for #[MapEntity] attribute, that lets you specify the function used by EntityValueResolver to inject the value right to route parameter.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.