I am a beginner on Symfony 6 and I am blocked because I have an error message: "Undefined method getDoctrine" with Intelephense
Here is my code:
#[Route('/recettes', name: 'app_recettes')]
public function index(int $id): Response
{
$em = $this->getDoctrine()->getManager();
$recette = $em->getRepository(Recettes::class)->findBy(['id' => $id]);
return $this->render('recettes/index.html.twig', [
'RecettesController' => 'RecettesController',
]);
}
3
Answers
Your controller should extends
AbstractController
fromuse SymfonyBundleFrameworkBundleControllerAbstractController;
You should not use
getDoctrine()->getManager()
in symfony 6. If you look into the method fromAbstractController
you can see:You should just autowire your entity manager in your method or constructor and use it directly.
You could also autowire your RecettesRepository directly instead of the entity manager if you just want to fetch some data.
I’m guessing that you want to show a specific resource by using its id. You probably want to add something
/{id}
in your route:Dylan response is really good !
If you want to fecth a specific recette (blog de cuisine?), you can autowire the ‘recette’ as an argument :
To do so, don’t forget to install and import :
use SensioBundleFrameworkExtraBundleConfigurationEntity;
In Symfony 6,
getDoctrine()
method have been removed. Instead of it you can use$doctrine
variable. To use it follow this steps.use this in your controller
pass this argument into the your index function
Access
$doctrine
variable like this.