Zanshin13
Zanshin13

Reputation: 1066

How to get Entity class name by relation name

I have 2 Entities City and Region
City linked to Region this way:

/**
 * @ORM\ManyToOne(targetEntity="Region", inversedBy="cities")
 * @var Region
 */
protected $region;

How can I get Region class name having only "region" string (and knowing that region is a relation of City)?

Upvotes: 0

Views: 699

Answers (1)

Yoshi
Yoshi

Reputation: 54659

On the entity/object manager (\Doctrine\Common\Persistence\ObjectManager) you can call getClassMetadata($className).

E.g.:

$metaData = $em->getClassMetadata(City::class);
$metaData->getAssociationTargetClass('region'); // => 'Region'

Upvotes: 1

Related Questions