symfony - Symfony 5 pb 与存储库
问题描述
我有以下错误: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;
}
}
解决方案
错误已经说明了:
<?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
需要它属于哪个实体类型,因此需要通过引用进行类型提示,我们需要将类名指定给构造函数。
推荐阅读
- r - 对数据框的不同列应用不同的函数,并对多个数据框重复相同的函数
- python - 截图后排列监视器结果
- java - 如何在不使用 Spring Boot 的情况下使用 Hibernate .xml 配置注入 Spring Data JPA?
- c# - ScriptableObject 泛型列表在统一序列化时使所有派生实例成为基类型
- apache - 一些 Jenkins URL 没有尾部斜杠并且代理服务器失败
- ruby-on-rails - 有没有办法限制特定用户访问某些应用程序?
- reactjs - 在 J2EE 应用程序中插入 React JS-JSX
- javascript - 在函数中更改对象的字段
- spring - @Before 建议匹配参数类型
- c++ - 大多数人代码中的 ONLINE_JUDGE 是什么?