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

我不知道为什么...

解决方法

已修复。请使用 Doctrine\Common\Util\ClassUtils::getClass 而不是 php 本机函数 'get_class'