微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Doctrine AnnotationReader 不适用于关系实体

如何解决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'

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。