我想反过来从实体获取表名:
我想从表名获取实体名称,即映射到该表的实体名称。
db列也是如此:如何获取映射到它的实体的字段名称?
答案 0 :(得分:4)
这是我能够做到的,虽然它不是最佳的,因为它遍历注册的所有实体类名称:
/**
* @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;
}