Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Symfony 5.4 – Call to undefined method App\Controller\SiteValdinguController::getRepository()

I’m new to Symfony and can’t seem to find a way to fix my issue.

I have done an earlier project in which I didn’t have this problem but it seems that the method getDoctrine is considered undefined.
enter image description here

here is the 1st route of my controller

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?php
namespace App\Controller;

use Doctrine\Persistence\ObjectManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension;
use Symfony\Component\Form\Forms;
use Symfony\Component\HttpFoundation\Request;

use App\Entity\Accueil;
use App\Entity\Actualite;
use App\Entity\Admin;
use App\Entity\Artistique;
use App\Entity\Avis;
use App\Entity\Equipe;
use App\Entity\Fonction;
use App\Entity\Image;
use App\Entity\Partenaire;
use App\Entity\TypeArtistique;


class SiteValdinguController extends AbstractController
{
    /**
     * @Route("/", name="app_site_valdingu")
     */
    public function index(Request $request, ManagerRegistry $entityManager): Response
    {
        unset($_POST['triArtNom']);
        unset($_POST['triArtNbRepres']);
        unset($_POST['triArtTypeArt']);

        unset($_POST['triActuNom']);
        unset($_POST['triActuDate']);
        unset($_POST['triActuTypeArt']);
        unset($_POST['triActuTime']);

        $repos = $this->getRepository(Accueil::class);
        $LesAccueils = $repos->findAll();

        $repos = $this->getRepository(Actualite::class);
        $LesActualites = $repos->findAll();

        $repos = $this->getRepository(Image::class);
        $LesImages = $repos->findAll();

        return $this->render('site_valdingu/index.html.twig', [
            'controller_name' => 'SiteValdinguController',
            'LesAccueils'=>$LesAccueils,
            'LesActualite'=>$LesActualites
        ]);
    }

Here is the relevant part of my Entity

namespace App\Entity;

use App\Repository\AccueilRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: AccueilRepository::class)]
class Accueil
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    private ?string $Label = null;

    #[ORM\Column(length: 255)]
    private ?string $Texte = null;

    #[ORM\OneToMany(mappedBy: 'Acc_id', targetEntity: Image::class)]
    private Collection $img;
`

and here is the relevant part of my Repository
`namespace App\Repository;

use App\Entity\Accueil;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @extends ServiceEntityRepository<Accueil>
 *
 * @method Accueil|null find($id, $lockMode = null, $lockVersion = null)
 * @method Accueil|null findOneBy(array $criteria, array $orderBy = null)
 * @method Accueil[]    findAll()
 * @method Accueil[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class AccueilRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Accueil::class);
    }

    public function save(Accueil $entity, bool $flush = false): void
    {
        $this->getEntityManager()->persist($entity);

        if ($flush) {
            $this->getEntityManager()->flush();
        }
    }

    public function remove(Accueil $entity, bool $flush = false): void
    {
        $this->getEntityManager()->remove($entity);

        if ($flush) {
            $this->getEntityManager()->flush();
        }
    }

I used Symfony 6 for my last project, and I thought about not having done the right translation in some places but I didn’t notice anything myself.

I also have weird things like no automatic annotations.yaml file created so maybe some routing stuff is messing up but I didn’t have do worry about it last time so it feels weird + it seems that it’s not the annotations routes that cause the problem since I’m technically on the right page, it just doesn’t work and can’t extract data from the db.

Both when I use the old getDoctrine()->getRepository() method with the EntityManagerInterface and the immediate getRepository() method with the ManagerRegistry give me the same result

The migrations work so it’s not a connexion to db problem.

>Solution :

You are calling $this->getRepository(), so it’s trying to use a method that does not exist in SiteValdinguController. What you are trying to achieve is getting each repository using the entity manager. So you need to call $managerRegistry->getRepository() instead.

The correct code would be:

    /**
     * @Route("/", name="app_site_valdingu")
     */
    public function index(Request $request, ManagerRegistry $entityManager): Response
    {
        unset($_POST['triArtNom']);
        unset($_POST['triArtNbRepres']);
        unset($_POST['triArtTypeArt']);

        unset($_POST['triActuNom']);
        unset($_POST['triActuDate']);
        unset($_POST['triActuTypeArt']);
        unset($_POST['triActuTime']);

        $repos = $entityManager->getRepository(Accueil::class);
        $LesAccueils = $repos->findAll();

        $repos = $entityManager->getRepository(Actualite::class);
        $LesActualites = $repos->findAll();

        $repos = $entityManager->getRepository(Image::class);
        $LesImages = $repos->findAll();

        return $this->render('site_valdingu/index.html.twig', [
            'controller_name' => 'SiteValdinguController',
            'LesAccueils'=>$LesAccueils,
            'LesActualite'=>$LesActualites
        ]);
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading