Symfony 5 pb 带存储库

问题描述 投票:0回答:1

我有以下错误: DefinitionErrorExceptionPass.php 第 54 行: 无法自动装配服务“App\Repository\EmployeeRepository”:方法“Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository::__construct()”的参数“$entityClass”没有类型提示,您应该配置其值
明确地。

我的课程:

<?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;
    }
}
symfony repository
1个回答
2
投票

错误已经说明了:

<?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
}

按照文档中所述,将正确的参数传递给构造函数。 查询对象:存储库

错误:

“Doctrine\Bundle\DoctrineBundle\Repository ServiceEntityRepository::__construct()" 没有类型提示

由于

ServiceEntityRepository
需要它属于哪个实体类型,因此需要通过引用进行类型提示,因此我们需要向构造函数指定类名。

© www.soinside.com 2019 - 2024. All rights reserved.