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?