php - Doctrine AnnotationReader 不适用于关系实体
问题描述
在我的 Symfony 5 项目(PHP 8)中,我设置了一个名为 Uploadable 的自定义注释。
以下是我检查实体上是否存在@Uploadable注释的方法:
use Doctrine\Common\Annotations\AnnotationReader;
class UploadAnnotationReader
{
private AnnotationReader $reader;
public function __construct(private EntityManagerInterface $manager){
$this->reader = new AnnotationReader();
}
/**
* @throws \ReflectionException
*/
public function isUploadable($entity):bool
{
$reflection = new \ReflectionClass(get_class($entity));
return $this->reader->getClassAnnotation($reflection, Uploadable::class) !== null;
}
当我传入参数时,直接从数据库或从 Symfony paramConverter 获得的实体(确实具有我创建的注释),然后 AnnotationReader 能够看到此注释。
另一方面,如果我通过另一个实体的关系取回这个实体,奇怪的是,它不再起作用了!
$entityA = $manager->getRepository(...)....;
$myService->isUploadable($entityA); // return true
$entityB = $manager->getRepository(...)....;
$myService->isUploadable($entityB->getEntityA()); // return false
$entityB = $manager->getRepository(...)....;
$entityA = $entityB->getEntityA();
$myService->isUploadable($entityB->getEntityA()); // return true
而且我不知道为什么...
解决方案
固定的。Juste 使用Doctrine\Common\Util\ClassUtils::getClass而不是 php 本机函数 ' get_class '
推荐阅读
- sql - 为什么 INSERT 在 Python SQLite3 中似乎不起作用?
- java - Java 8 或更高版本,从 char 数组中删除值
- timer - 使用 ICU - Atmega32 测量生成的波频率
- javascript - 在 Javascript 中,有没有一种方法可以在不使用循环的情况下多次连接字符串?
- html - request.user 用于匿名用户还是不在 django 中?
- c++ - 您能否在不使用 GetAsyncKeyState 或线程的情况下同时获取用户输入,同时在 C++ 中为 Windows 程序运行函数?
- python - 如何获取数组中的数据
- r - R:根据字母成绩计算学生 GPA 的 R 脚本
- node.js - 如何将 MongoDB-atlas 集合中的特定文档与存储在同一集群中不同集合中的文档相关联?
- python - 从具有多个数组的函数创建矩阵而不使用 for 循环