Symfony error: The class xxx was not found in the chain configured namespaces App\Entity

Symfony error: The class xxx was not found in the chain configured namespaces App\Entity

我用一个实体 Company 和一个控制器 CompanyController 创建了一个新的 symfony 项目。我想从数据库中获取结果,但我一直收到此错误:The class 'App\Repository\CompanyRepository' was not found in the chain configured namespaces App\Entity,我不知道为什么。

我在互联网上进行了搜索,但我只阅读了在命名空间不是 App\Entity 时解决错误的答案。请帮助我。

创建新的symfony 项目时,所有文件都存储在src 文件夹中。我没有更改任何配置文件,所以每个配置都是默认的。

这是我的实体:

<?php

namespace App\Entity;

use App\Repository\CompanyRepository;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=CompanyRepository::class)
 */
class Company

之后只有 getter 和 setter。

这是我的控制器:

<?php

namespace App\Controller;

use App\Repository\CompanyRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

#[Route('/api/company', name: 'company')]
class CompanyController extends AbstractController
{
    #[Route(name: 'company.get', methods: ["GET"])]
    public function getCompanies(): Response
    {
        $entityManager = $this->getDoctrine()->getManager();
        $repository = $entityManager->getRepository(CompanyRepository::class);

        $companies = $repository->findAll();
        $data = [];

        foreach ($companies as $company) {
            $data[] = $company->toArray();
        }

        return $this->json([
            'data' => $data
        ]);
    }
}

这是我的公司资料库:

<?php

namespace App\Repository;

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

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

    // /**
    //  * @return Company[] Returns an array of Company objects
    //  */
    /*
    public function findByExampleField($value)
    {
        return $this->createQueryBuilder('c')
            ->andWhere('c.exampleField = :val')
            ->setParameter('val', $value)
            ->orderBy('c.id', 'ASC')
            ->setMaxResults(10)
            ->getQuery()
            ->getResult()
        ;
    }
    */

    /*
    public function findOneBySomeField($value): ?Company
    {
        return $this->createQueryBuilder('c')
            ->andWhere('c.exampleField = :val')
            ->setParameter('val', $value)
            ->getQuery()
            ->getOneOrNullResult()
        ;
    }
    */
}

在控制器中,将 $repository = $entityManager->getRepository(CompanyRepository::class); 替换为
$repository = $entityManager->getRepository(Company::class);

并将use App\Repository\CompanyRepository;替换为use App\Entity\Company;

因为 getRepository() 需要实体 class,而不是存储库 class。

<?php

namespace App\Controller;

use App\Entity\Company;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

#[Route('/api/company', name: 'company')]
class CompanyController extends AbstractController
{
    #[Route(name: 'company.get', methods: ["GET"])]
    public function getCompanies(): Response
    {
        $entityManager = $this->getDoctrine()->getManager();
        $repository = $entityManager->getRepository(Company::class);

        $companies = $repository->findAll();
        $data = [];

        foreach ($companies as $company) {
            $data[] = $company->toArray();
        }

        return $this->json([
            'data' => $data
        ]);
    }
 }