Bertou19
Bertou19

Reputation: 27

Undefined method getDoctrine

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',
        ]);
    }

Upvotes: 0

Views: 4030

Answers (4)

M. Arslan Hafeez
M. Arslan Hafeez

Reputation: 131

As of Symfony 5.4, the getDoctrine() method in the abstract controller is deprecated, and it's removed in Symfony 6.

You've already injected the manager registry into your controller action, so replace the $this->getDoctrine() call with the $doctrine variable.

Like this:

// src/Controller/IndexController.php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\Persistence\ManagerRegistry;

class IndexController extends AbstractController
{
    private $doctrine;

    public function __construct(ManagerRegistry $doctrine)
    {
        $this->doctrine = $doctrine;
    }

    /**
     * @Route("/", name="homepage")
     */
    public function index(): Response
    {
        // Access Doctrine using $this->doctrine
        $entityManager = $this->doctrine->getManager();

        // Your controller logic here
        return $this->render('index.html.twig');
    }
}

Upvotes: 0

kalpana udara
kalpana udara

Reputation: 71

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

use Doctrine\Persistence\ManagerRegistry as PersistenceManagerRegistry;

pass this argument into the your index function

public function index(int $id,  PersistenceManagerRegistry $doctrine): Response

Access $doctrine variable like this.

$em = $doctrine->getManager();

Upvotes: 0

fatoon
fatoon

Reputation: 23

Dylan response is really good !

If you want to fecth a specific recette (blog de cuisine?), you can autowire the 'recette' as an argument :

#[Route('/recettes/{id}', name: 'app_recettes')]
public function index(Recette $recette): Response
{
    return $this->render('recettes/index.html.twig', [
         'recette' => $recette,
     ]);

}

To do so, don't forget to install and import :

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;

Upvotes: 0

Dylan KAS
Dylan KAS

Reputation: 5663

Your controller should extends AbstractController from use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

You should not use getDoctrine()->getManager() in symfony 6. If you look into the method from AbstractController you can see:

trigger_deprecation('symfony/framework-bundle', '5.4', 'Method "%s()" is deprecated, inject an instance of ManagerRegistry in your controller instead.', __METHOD__);

You should just autowire your entity manager in your method or constructor and use it directly.

private EntityManagerInterface $entityManager;

public function __construct(EntityManagerInterface $entityManager)
{
    $this->entityManager = $entityManager;
}

#[Route('/recettes', name: 'app_recettes')]
public function index(int $id): Response
{
    $recette = $this->entityManager->getRepository(Recettes::class)->findBy(['id' => $id]);

    return $this->render('recettes/index.html.twig', [
        'RecettesController' => 'RecettesController',
    ]);
}

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:

#[Route('/recettes/{id}', name: 'app_recettes')]

Upvotes: 4

Related Questions