PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 4, 2022

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

 March 04, 2022     doctrine, php, symfony     No comments   

Issue

I created a new symfony project with an entity Company and a contoller CompanyController. I want to get the results from a database but I keep getting this error: The class 'App\Repository\CompanyRepository' was not found in the chain configured namespaces App\Entity and I don't know why.

I searched the internet but I read only answers that are solving errors when the namespace is not App\Entity. Please help me.

The files are all stored in the src folder as it is when creating a new symfony project. I didn't change any configuration files so every configuration is on default.

Here is my entity:

<?php

namespace App\Entity;

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

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

After that there are just getter and setter.

Here is my controller:

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

Here is my Company Repository:

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


Solution

In the controller, replace $repository = $entityManager->getRepository(CompanyRepository::class); with
$repository = $entityManager->getRepository(Company::class);

And replace use App\Repository\CompanyRepository; with use App\Entity\Company;

Because getRepository() expects the Entity class, instead of the Repository 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
        ]);
    }
 }


Answered By - Ouss Ma L'aire Bien
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing