Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 5 pb with Repository

I have the following error: DefinitionErrorExceptionPass.php line 54: Cannot autowire service "App\Repository\EmployeeRepository": argument "$entityClass" of method "Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository::__construct()" has no type-hint, you should configure its value
explicitly.

My classes:

<?php
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\EmployeeRepository")
 */
class Employee extends User
{

}

<?php

namespace App\Controller;


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

class EmployeeController extends AbstractController
{
    /**
     * @return Response
     * @Route('/employees')
     */
    public function getEmployees(): Response
    {
        $employees = $this->getDoctrine()->getRepository(Employee::class)->findAll();

        if(!$employees){
            return new Response(null);
        }

        $formatted = [];
        foreach ($employees as $employee){
            $formatted[] = $this->getDoctrine()->getRepository(Employee::class)->getDetails($employee);
        }

        return new Response($formatted);
    }

<?php

namespace App\Repository;

use App\Entity\Employee;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;

class EmployeeRepository extends ServiceEntityRepository
{
    public function getDetails(Employee $employee)
    {
        return [
            'id'        => $employee->getId(),
            'firstname' => $employee->getFirstname(),
            'lastname'  => $employee->getLastname(),
            'email'     => $employee->getEmail(),
            'password'  => $employee->getPassword(),
            'startDate' => $employee->getStartDate(),
            'endDate'   => $employee->getEndDate(),
        ];
    }
}

<?php
namespace App\Entity;

use Doctrine\ORM\Mapping\MappedSuperclass;
use Doctrine\ORM\Mapping as ORM;
use DateTime;

/**
 * @MappedSuperclass
 */
class User
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue()
     */
    private $id;

    /**
     * @ORM\Column(type="string")
     */
    private $firstname;

    /**
     * @ORM\Column(type="string")
     */
    private $lastname;

    /**
     * @ORM\Column(type="string")
     */
    private $email;

    /**
     * @ORM\Column(type="string")
     */
    private $password;

    /**
     * @ORM\Column(type="datetime")
     */
    private $startDate;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $endDate;

    /**
     * @param int $id
     */
    public function setId(int $id)
    {
        $this->id = $id;
    }

    /**
     * @return int
     */
    public function getId(): int
    {
        return $this->id;
    }

    /**
     * @param string $email
     */
    public function setEmail(string $email)
    {
        $this->email = $email;
    }

    /**
     * @return string
     */
    public function getPassword(): string
    {
        return $this->password;
    }

    /**
     * @return string
     */
    public function getEmail(): string
    {
        return $this->email;
    }

    /**
     * @return string
     */
    public function getLastname():string
    {
        return $this->lastname;
    }

    /**
     * @return string
     */
    public function getFirstname(): string
    {
        return $this->firstname;
    }

    /**
     * @param string $password
     */
    public function setPassword(string $password)
    {
        $this->password = $password;
    }

    /**
     * @param string $lastname
     */
    public function setLastname(string $lastname)
    {
        $this->lastname = $lastname;
    }

    /**
     * @param string $firstname
     */
    public function setFirstname(string $firstname)
    {
        $this->firstname = $firstname;
    }

    /**
     * @return DateTime
     */
    public function getEndDate(): ?DateTime
    {
        return $this->endDate;
    }

    /**
     * @return DateTime
     */
    public function getStartDate(): DateTime
    {
        return $this->startDate;
    }

    /**
     * @param DateTime $endDate
     */
    public function setEndDate(?DateTime $endDate)
    {
        $this->endDate = $endDate;
    }

    /**
     * @param DateTime $startDate
     */
    public function setStartDate(DateTime $startDate)
    {
        $this->startDate = $startDate;
    }
}
like image 505
Philippe de Chabot Avatar asked Sep 17 '25 01:09

Philippe de Chabot


1 Answers

The error is saying it already :

<?php

namespace App\Repository;

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

class EmployeeRepository extends ServiceEntityRepository {

    public function __construct(ManagerRegistry $registry) {
        parent::__construct($registry, Employee::class);
    }
    // Other code
}

Pass proper arguments to constructor as requierd as stated in docs. Querying for Objects: The Repository

The Error :

"Doctrine\Bundle\DoctrineBundle\Repository ServiceEntityRepository::__construct()" has no type-hint

As ServiceEntityRepository requires which entity type it belongs to, thus requiring type hinting by reference, we need to specify the class name to constructor.

like image 115
Maulik Parmar Avatar answered Sep 18 '25 18:09

Maulik Parmar