【问题标题】:Symfony 5.4 - Call to undefined method App\Controller\SiteValdinguController::getRepository()Symfony 5.4 - 调用未定义的方法 App\\Controller\\SiteValdinguController::getRepository()
【发布时间】:2023-02-09 22:18:23
【问题描述】:

我是 Symfony 的新手,似乎找不到解决问题的方法。

我做过一个早期的项目,在这个项目中我没有遇到这个问题,但似乎 getDoctrine 方法被认为是未定义的。 enter image description here

这是我的控制器的第一条路线

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

这是我实体的相关部分

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();
        }
    }

我在上一个项目中使用了 Symfony 6,我想在某些地方没有做正确的翻译,但我自己没有注意到任何事情。

我也有一些奇怪的事情,比如没有自动创建 annotations.yaml 文件,所以可能一些路由的东西搞砸了,但我上次没有担心它,所以感觉很奇怪 + 似乎不是注释路由导致了问题因为我在技术上是在正确的页面上,所以它不起作用并且无法从数据库中提取数据。

当我将旧的 getDoctrine()->getRepository() 方法与 EntityManagerInterface 和直接的 getRepository() 方法与 ManagerRegistry 一起使用时,都会给我相同的结果

迁移有效,因此它与数据库问题无关。

【问题讨论】:

    标签: php symfony methods repository undefined


    【解决方案1】:

    您正在调用$this-&gt;getRepository(),因此它试图使用SiteValdinguController 中不存在的方法。您想要实现的是使用实体管理器获取每个存储库。所以你需要打电话给$managerRegistry-&gt;getRepository()

    正确的代码是:

        /**
         * @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
            ]);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-13
      • 2020-07-27
      • 1970-01-01
      • 2021-07-22
      • 1970-01-01
      • 1970-01-01
      • 2017-06-10
      • 1970-01-01
      相关资源
      最近更新 更多