Symfony2/Doctrine2:如何访问实体注释映射?

Symfony2/Doctrine2:如何访问实体注释映射?,symfony,doctrine-orm,annotations,mapping,accessor,Symfony,Doctrine Orm,Annotations,Mapping,Accessor,在我的Symfony2/doctrine2应用程序中,我有两个实体:媒体和配方 它们可以通过一个或多个协会联系起来 在oneToMany关系的情况下,我使用以下代码检索链接到媒体实例的配方: $accessor = PropertyAccess::createPropertyAccessor(); $reflect = new ReflectionClass($media); $shortName = $reflect->getShortName(); $value = $accesso

在我的Symfony2/doctrine2应用程序中,我有两个实体:媒体和配方

它们可以通过一个或多个协会联系起来

在oneToMany关系的情况下,我使用以下代码检索链接到媒体实例的配方:

$accessor = PropertyAccess::createPropertyAccessor();
$reflect = new ReflectionClass($media);
$shortName =  $reflect->getShortName();
$value = $accessor->getValue($element, $shortName);
但是,如果关系是多个,并且如果我为属性指定了自定义名称,则前面的代码不起作用

如何通过编程从媒体类的注释映射中检索mappedBy的配方

/**
 * @ORM\OrderBy({"sortablePosition" = "ASC"})
 * @Assert\Valid()
 * @ORM\ManyToMany(targetEntity="\AppBundle\Entity\Core\Media", mappedBy="recipes", cascade={"persist", "remove"})
 */
protected $medias;

您需要的是一个实现条令\公共\注释\读取器接口的类。它已注册为批注读取器服务。使用此类,您可以使用诸如getClassAnnotation、getMethodAnnotations等方法获取各种对象的注释。在您的情况下,getPropertyAnnotations似乎是一个不错的选择:

$reflClass = new \ReflectionClass($class); //$class is an instance of your entity
$refProp = $reflClass->getProperty('medias');
$annotations = $reader->getPropertyAnnotations($refProp);

$annotations是注释的集合。在您的情况下,将有3个元素。检查更多信息

您需要的是一个实现条令\公共\注释\读者界面的类。它已注册为批注读取器服务。使用此类,您可以使用诸如getClassAnnotation、getMethodAnnotations等方法获取各种对象的注释。在您的情况下,getPropertyAnnotations似乎是一个不错的选择:

$reflClass = new \ReflectionClass($class); //$class is an instance of your entity
$refProp = $reflClass->getProperty('medias');
$annotations = $reader->getPropertyAnnotations($refProp);

$annotations是注释的集合。在您的情况下,将有3个元素。检查更多信息

您可以从实体的元数据接收有关映射的信息

$metadata = $this->getDoctrine()
   ->getManager()
   ->getMetadataFactory()
   ->getMetadataFor(\Doctrine\Common\Util\ClassUtils::getClass($object))
;

您可以从实体的元数据接收有关映射的信息

$metadata = $this->getDoctrine()
   ->getManager()
   ->getMetadataFactory()
   ->getMetadataFor(\Doctrine\Common\Util\ClassUtils::getClass($object))
;