如何在Image监听器中获取关联的UserProfile类及UploadField注解
解决方案
一、在生命周期监听器中获取UserProfile并读取注解
方法1:利用双向关联直接获取
先确保Image实体定义了反向关联到UserProfile的属性(这是最简洁的方式):
// src/Entity/Image.php use Doctrine\ORM\Mapping as ORM; use App\Entity\UserProfile; /** * @ORM\Entity */ class Image { // 其他属性... /** * @ORM\OneToOne(mappedBy="image", targetEntity=UserProfile::class) */ private ?UserProfile $userProfile = null; public function getUserProfile(): ?UserProfile { return $this->userProfile; } }
接着在监听器中直接获取关联的UserProfile,再读取$image属性上的注解:
// src/EventListener/ImageListener.php use Doctrine\Persistence\Event\LifecycleEventArgs; use App\Entity\Image; use Doctrine\Common\Annotations\Reader; class ImageListener { private $annotationReader; public function __construct(Reader $annotationReader) { $this->annotationReader = $annotationReader; } public function prePersist(LifecycleEventArgs $args) { $entity = $args->getObject(); if (!$entity instanceof Image) return; $userProfile = $entity->getUserProfile(); if (!$userProfile) return; // 获取UserProfile类中$image属性的反射对象 $reflectionClass = new \ReflectionClass($userProfile); $reflectionProperty = $reflectionClass->getProperty('image'); // 读取@UploadField注解 $uploadFieldAnnotation = $this->annotationReader->getPropertyAnnotation( $reflectionProperty, \Verzeilberg\UploadImagesBundle\Annotation\UploadField::class ); if ($uploadFieldAnnotation) { $mapping = $uploadFieldAnnotation->mapping; // 此处拿到"profile_image" // 执行后续业务逻辑 } } }
方法2:无反向关联时,通过Doctrine UnitOfWork查找关联
如果未定义反向关联,可通过UnitOfWork遍历所有实体,找出关联当前Image的UserProfile:
public function prePersist(LifecycleEventArgs $args) { $entity = $args->getObject(); if (!$entity instanceof Image) return; $em = $args->getEntityManager(); $uow = $em->getUnitOfWork(); // 遍历所有实体类的关联映射 foreach ($uow->getIdentityMap() as $entityClass => $entities) { $classMetadata = $em->getClassMetadata($entityClass); foreach ($classMetadata->getAssociationMappings() as $assocName => $assocMapping) { // 筛选关联到Image的OneToOne关系 if ($assocMapping['targetEntity'] === Image::class && $assocMapping['type'] === \Doctrine\ORM\Mapping\ClassMetadataInfo::ONE_TO_ONE) { foreach ($entities as $relatedEntity) { $relatedImage = $classMetadata->getFieldValue($relatedEntity, $assocName); if ($relatedImage === $entity) { // 找到对应的UserProfile,读取注解逻辑同方法1 $reflectionClass = new \ReflectionClass($relatedEntity); $reflectionProperty = $reflectionClass->getProperty($assocName); $uploadFieldAnnotation = $this->annotationReader->getPropertyAnnotation( $reflectionProperty, \Verzeilberg\UploadImagesBundle\Annotation\UploadField::class ); if ($uploadFieldAnnotation) { $mapping = $uploadFieldAnnotation->mapping; // 执行后续逻辑 } } } } } } }
二、在ImageAnnotationReader中获取UserProfile类
如果是自定义的注解阅读器类,可通过以下方式查找关联实体并读取注解:
// src/Annotation/ImageAnnotationReader.php use Doctrine\ORM\EntityManagerInterface; use Doctrine\Common\Annotations\Reader; class ImageAnnotationReader { private $em; private $annotationReader; public function __construct(EntityManagerInterface $em, Reader $annotationReader) { $this->em = $em; $this->annotationReader = $annotationReader; } public function getUploadMappingForImage($image): ?string { $imageClass = \Verzeilberg\UploadImagesBundle\Entity\Image::class; if (!$image instanceof $imageClass) return null; // 优先使用反向关联获取 if (method_exists($image, 'getUserProfile')) { $userProfile = $image->getUserProfile(); if ($userProfile) { $reflectionClass = new \ReflectionClass($userProfile); $reflectionProperty = $reflectionClass->getProperty('image'); $annotation = $this->annotationReader->getPropertyAnnotation( $reflectionProperty, \Verzeilberg\UploadImagesBundle\Annotation\UploadField::class ); return $annotation?->mapping; } } // 遍历所有实体查找关联 $metadataFactory = $this->em->getMetadataFactory(); foreach ($metadataFactory->getAllMetadata() as $classMetadata) { foreach ($classMetadata->getAssociationMappings() as $assocName => $assocMapping) { if ($assocMapping['targetEntity'] === $imageClass && $assocMapping['type'] === \Doctrine\ORM\Mapping\ClassMetadataInfo::ONE_TO_ONE) { $entities = $this->em->getRepository($classMetadata->getName())->findAll(); foreach ($entities as $entity) { $relatedImage = $classMetadata->getFieldValue($entity, $assocName); if ($relatedImage === $image) { $reflectionProperty = $classMetadata->getReflectionProperty($assocName); $annotation = $this->annotationReader->getPropertyAnnotation( $reflectionProperty, \Verzeilberg\UploadImagesBundle\Annotation\UploadField::class ); return $annotation?->mapping; } } } } } return null; } }
内容的提问来源于stack exchange,提问作者ViceStar
相关产品推荐
相关产品推荐

