Reputation: 49533
I would like to do the reverse of getting the table name from an entity:
I want to get the entity name from a table name, i.e. the name of the entity that is mapped to that table.
Same goes for the db column: how to get the field name of the entity that is mapped to it?
Upvotes: 6
Views: 3061
Reputation: 49533
Here is what I was able to do, though it's not optimum because it goes through all entity class names registered:
/**
* @param \Doctrine\ORM\EntityManager $em Entity manager
* @param string $table Table name
* @return string Entity class name, null if not found
*/
protected function getClassNameFromTableName($em, $table)
{
// Go through all the classes
$classNames = $em->getConfiguration()->getMetadataDriverImpl()->getAllClassNames();
foreach ($classNames as $className) {
$classMetaData = $em->getClassMetadata($className);
if ($table == $classMetaData->getTableName()) {
return $classMetaData->getName();
}
}
return null;
}
/**
* @param \Doctrine\ORM\EntityManager $em Entity manager
* @param string $className
* @param string $column
* @return string Field name, null if not found
*/
protected function getFieldNameFromColumnName($em, $className, $column)
{
$classMetaData = $em->getClassMetadata($className);
if ($classMetaData) {
return $classMetaData->getFieldForColumn($column);
}
return null;
}
Upvotes: 7